how can I create a grid from a while loop

0

I need to create a grid of 8 elements with the name of the items, the quantity and the price with random numbers, I have done this, but I do not know how to use the while loop to form the grid.

<table>
<tr><td>Nombre</td><td>Cantidad</td><td>Precio</td></tr>

<?php
$a = 0
while($a<=8){
echo "<tr>
         <td>Nombre".$a."</td>
         <td>50</td>
         <td>".(rand(1,100))."</td>
      </tr>";
      a = ++;
}
?>

The loop has to be yes or if a while does not allow me to use for.

    
asked by Sora Kasugano 15.04.2018 в 23:50
source

1 answer

0

Comments regarding your code:

  • in the line where you declare the variable $ a you needed to put the semicolon
  • One of the ways to do the increase of one plus one is $ a ++ and you had it as $ a = ++ which is an incorrect syntax li>

    Therefore to generate your table with random values your own code would be like this

    <?php
    $a = 0;
    while($a<=8){
    echo "<tr>
             <td>Nombre".$a."</td>
             <td>50</td>
             <td>".(rand(1,100))."</td>
          </tr>";
    $a++;
    }
    

    Now to add the tags that it opens and closes could be like this:

    <?php
    $a = 0;
    echo "<table> <tr><th>Nombre</th><th>Cantidad</th><th>Precio</th></tr>";
    while($a<=8){
    echo "<tr>
             <td>Nombre".$a."</td>
             <td>50</td>
             <td>".(rand(1,100))."</td>
          </tr>";
    $a++;
    }
    echo "</table>";
    
        
  • answered by 16.04.2018 в 00:25