Guest User

Untitled

a guest
Nov 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1.       //the array
  2.         $table = array();
  3.  
  4.         //if the users picks numbers grab from form
  5.         if (isset($_POST["coords"])) {
  6.             $td1 = $_POST["td1"];
  7.             $td2 = $_POST["td2"];
  8.         //if not generate two randoms
  9.         } else {
  10.             $td1 = rand(1, 10);
  11.             $td2 = rand(1, 10);
  12.         }
  13.         echo $td1 . " " . $td2;
  14.  
  15.         //generate a 2 dimensional array
  16.         for ($i = 0; $i <= 10; $i++) {
  17.             for ($n = 0; $n <= 10; $n++) {
  18.  
  19.                 //find a 'O' or 'X'
  20.                 $rand = rand(0, 1);
  21.  
  22.                 //make a cell red
  23.                 if ($i == $td1 && $n == $td2) {
  24.                     if ($rand == 0) {
  25.                         $table[$i][$n] = "<span style='background:red;'> X </span>";
  26.                     } else {
  27.                         $table[$i][$n] = "<span style='background:red;'>O</span>";
  28.                     }
  29.                 }
  30.  
  31.                 //make the rest defaults
  32.                 elseif ($rand == 0) {
  33.                     $table[$i][$n] = "X";
  34.                 } else {
  35.                     $table[$i][$n] = "O";
  36.                 }
  37.             }
  38.         }
  39.  
  40.         //print the arrays
  41.         echo "<pre>";
  42.         //print_r($table);
  43.         echo "</pre>";
  44.         //shuffle the array
  45.         //start the table
  46.         echo "<table style='float:left; margin-right:20px;' border='1'>";
  47.         echo "<tr>";
  48.         echo "<td></td>";
  49.         for ($td = 0; $td <= 10; $td++) {
  50.             echo "<td>$td</td>";
  51.         }
  52.         echo "</tr>";
  53.  
  54.         //echo the tables
  55.         foreach ($table as $row => $col) {
  56.             echo "<tr><td>$row</td>";
  57.  
  58.             foreach ($col as $cell) {
  59.                 echo "<td>$cell</td>";
  60.             }
  61.  
  62.             echo '</tr>';
  63.         }
  64.  
  65.         echo "</table>";
  66.         ?>
  67.         Pick the red field
  68.         <form action="" method="POST">
  69.             <input type="number" value="<?php echo $_POST["td1"]; ?>" name="td1" style="width:50px;"/>
  70.             x
  71.             <input type="number" value="<?php echo $_POST["td2"]; ?>" name="td2" style="width:50px;"/>
  72.             <input type="submit" value="Pick it!" name="coords" />
  73.         </form>
Add Comment
Please, Sign In to add comment