Html table from random sequence in for loop

Since I'm a music nerd, I created a little script to create a random rhythmic pattern:

echo "X "; for ($beats=rand(0,11); $beats>0; $beats--){ $xo=rand(0,2); if ($xo==0){ echo "x "; } else { echo "- "; } } 

It gives a random rhythm of up to 12 beats, where “x” stands for shock, and the first beat is always emphasized. (Output Example: Xxx -)

Now, for the sake of views, I would like to put this data in an html table. I would like the markup for the above example to be like this:

 <table border="1"> <tr> <th>Beat:</th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> </tr> <tr> <td>Accent:</td> <td>X</td> <td>-</td> <td>x</td> <td>-</td> <td>x</td> <td>-</td> <td>-</td> </tr> </table> 

Alas, my programming skill ends here. Can anyone suggest some code to help with this?

+4
source share
2 answers
 <table border='1'> <tr> <th>Beat:</th> <? $times = rand(1,12); $i = 1; while ($i <= $times) { echo "<th>$i</th>"; $i++; } ?> </tr> <tr> <td>Accent:</td> <td>X</td> <? $i = 1; while ($i <= ($times-1)) { if (rand(0,1)) { echo "<td>x</td>";} else {echo "<td>-</td>";} $i++; } ?> </tr> </table> 
+1
source

Just generate a template inside the table:

 <?php $numBeats=rand(0,11); ?> <table border="1"> <tr> <?php echo "<td>Beat:</td>"; for ($i=1; $i<=$numBeats+1; $i++){ echo "<td>" . $i . "</td>"; } ?> </tr> <tr> <?php echo "<td>Accent:</td>"; echo "<td>X</td>"; for ($beats=$numBeats; $beats>0; $beats--){ $xo=rand(0,2); if ($xo==0){ echo "<td>x</td>"; } else { echo "<td>-</td>"; } } ?> </tr> </table> 
+2
source

Source: https://habr.com/ru/post/1484930/


All Articles