Advertisement
Sanddru

Untitled

Oct 5th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.80 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "slagskib";
  7.  
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9.  
  10. if ($conn->connect_error) {
  11.     //die("Connection failed: " . $conn->connect_error);
  12. }
  13.  
  14. ?>
  15.  
  16. <html>
  17.   <head>
  18.     <title>Play battleships</title>
  19.     <style>
  20.       table.map {
  21.         border-collapse: collapse;
  22.         display: inline-block;
  23.       }
  24.       table.map td, th {
  25.         border: 3px solid #ccc;
  26.         width: 40px;
  27.         height: 20px;
  28.         text-align: center;
  29.       }
  30.       table.map td.nul, th {
  31.         background-color: #eee;
  32.       }
  33.       td.ramt {
  34.         background-color: #DC143C;
  35.       }
  36.       td.ship {
  37.         background-color: #555;
  38.       }
  39.       td form {
  40.         width: 100%;
  41.         height: 100%;
  42.       }
  43.     </style>
  44.   </head>
  45. </html>
  46.  
  47. <?php
  48. echo "    <table class='map'>\n";
  49. echo "      <tr>\n";
  50. echo "        <td class='nul'></td>\n";
  51. // udskriv kolonne-bogstaver over kortet
  52. for($x = 'A'; $x <= 'J'; $x++ ){
  53.  echo "        <td class='nul'>$x</td>\n";
  54. }
  55. echo "      </tr>\n";
  56.  
  57. for($y = 1; $y <= 10; $y++) {
  58.  echo "      <tr>\n";
  59.   // udskriv rækkens nummer til venstre for kortet
  60.   echo "        <td class='nul'>$y</td>\n";
  61.   //udfyld alle celler
  62.   for($x = 1; $x <= 10; $x++ ){
  63.    echo "        <td";
  64.  
  65.  
  66.  
  67.    $sejedata = "SELECT * from skud where x = '$x' and y = '$y'";
  68.    $skibsdata = "SELECT * from skip_pos where x = '$x' and y = '$y'";
  69.    $minposition = $conn->query($sejedata);
  70.     $skudposition = $conn->query($skibsdata);
  71.     if ($skudposition -> num_rows > 0){
  72.     if($minposition -> num_rows > 0){
  73.       echo " class = ramt ";
  74.     }
  75.     }
  76.     else if ($minposition -> num_rows > 0){
  77.         echo " class = ship ";
  78.       }
  79.     else if($skudposition -> num_rows > 0){
  80.         echo "x";
  81.  
  82.       }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.     echo ">";
  89.     echo "          <form method='GET' onclick='submitForm(this)'>\n";
  90.     echo "            <input type='hidden' name='maptype' value='my'>\n";
  91.     echo "            <input type='hidden' name='x' value='$x'>\n";
  92.     echo "            <input type='hidden' name='y' value='$y'>\n";
  93.     echo "          </form>\n";
  94.     echo "</td>\n";
  95. }
  96. }
  97.  
  98.     if(isset($_GET["x"])) {
  99.       $x=$_GET["x"];
  100.       $y=$_GET["y"];
  101.       $omramt = "SELECT * from skip_pos where x = '$x' and y = '$y' ";
  102.       $myresort = $conn->query($omramt);
  103.       if ($myresort -> num_rows > 0){
  104.         $mydata = "INSERT INTO skud (x,y,miss,plate_id) VALUES('$x','$y','false','0')";
  105.         $conn->query($mydata);
  106.       }
  107.       else if ($myresort -> num_rows <= 0) {
  108.        $mydata = "INSERT INTO skud (x,y,miss,plate_id) VALUES('$x','$y','true','0')";
  109.        $conn ->query($mydata);
  110.  
  111.  
  112. }
  113.  
  114.   }
  115.  
  116. ?>
  117. <script>
  118.  function submitForm(e){
  119.    e.submit();
  120.  
  121. }
  122. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement