Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "slagskib";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- //die("Connection failed: " . $conn->connect_error);
- }
- ?>
- <html>
- <head>
- <title>Play battleships</title>
- <style>
- table.map {
- border-collapse: collapse;
- display: inline-block;
- }
- table.map td, th {
- border: 3px solid #ccc;
- width: 40px;
- height: 20px;
- text-align: center;
- }
- table.map td.nul, th {
- background-color: #eee;
- }
- td.ramt {
- background-color: #DC143C;
- }
- td.ship {
- background-color: #555;
- }
- td form {
- width: 100%;
- height: 100%;
- }
- </style>
- </head>
- </html>
- <?php
- echo " <table class='map'>\n";
- echo " <tr>\n";
- echo " <td class='nul'></td>\n";
- // udskriv kolonne-bogstaver over kortet
- for($x = 'A'; $x <= 'J'; $x++ ){
- echo " <td class='nul'>$x</td>\n";
- }
- echo " </tr>\n";
- for($y = 1; $y <= 10; $y++) {
- echo " <tr>\n";
- // udskriv rækkens nummer til venstre for kortet
- echo " <td class='nul'>$y</td>\n";
- //udfyld alle celler
- for($x = 1; $x <= 10; $x++ ){
- echo " <td";
- $sejedata = "SELECT * from skud where x = '$x' and y = '$y'";
- $skibsdata = "SELECT * from skip_pos where x = '$x' and y = '$y'";
- $minposition = $conn->query($sejedata);
- $skudposition = $conn->query($skibsdata);
- if ($skudposition -> num_rows > 0){
- if($minposition -> num_rows > 0){
- echo " class = ramt ";
- }
- }
- else if ($minposition -> num_rows > 0){
- echo " class = ship ";
- }
- else if($skudposition -> num_rows > 0){
- echo "x";
- }
- echo ">";
- echo " <form method='GET' onclick='submitForm(this)'>\n";
- echo " <input type='hidden' name='maptype' value='my'>\n";
- echo " <input type='hidden' name='x' value='$x'>\n";
- echo " <input type='hidden' name='y' value='$y'>\n";
- echo " </form>\n";
- echo "</td>\n";
- }
- }
- if(isset($_GET["x"])) {
- $x=$_GET["x"];
- $y=$_GET["y"];
- $omramt = "SELECT * from skip_pos where x = '$x' and y = '$y' ";
- $myresort = $conn->query($omramt);
- if ($myresort -> num_rows > 0){
- $mydata = "INSERT INTO skud (x,y,miss,plate_id) VALUES('$x','$y','false','0')";
- $conn->query($mydata);
- }
- else if ($myresort -> num_rows <= 0) {
- $mydata = "INSERT INTO skud (x,y,miss,plate_id) VALUES('$x','$y','true','0')";
- $conn ->query($mydata);
- }
- }
- ?>
- <script>
- function submitForm(e){
- e.submit();
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement