Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Because I am a music nerd, I've made a little script to generate 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' indicates an accented beat, with the first beat always accented. (Example output: X-x-x--)

Now, for looks, I'd like to place this data into an html table. I would like the markup for the example above to be like so:

<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, this is where my programming skill ends. Can anyone offer some code to help with this?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
<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>
share|improve this answer
 
This is VERY close! At first glance it is lacking the 'lines' of the table, and starts the accents directly under the word 'Beat' with no 'accent' box. –  Gregory Tippett Jun 6 '13 at 23:15
 
Right, I forgot about the "Accent" label at the start of the second row - now it should be fine. –  Ziarno Jun 6 '13 at 23:17
 
Ziarno, you are a true renaissance man or woman. I shall sing your praises to serialism geeks far and wide! –  Gregory Tippett Jun 6 '13 at 23:22
add comment

Just generate the pattern inside a 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>
share|improve this answer
 
Not quite - the "Beat" row also has to have a number of columns to correspond with the generated accent marks. Run the code and you'll see what I mean. –  Gregory Tippett Jun 6 '13 at 22:56
 
It should work now. –  Logan Jun 6 '13 at 23:01
 
$beats is rand(0,11) so table can have different number of columns - sometimes 7, sometimes more, sometimes less. –  furas Jun 6 '13 at 23:04
 
Sorry - to clarify - the number of th tags (the top column) should vary along with the number of beats. So if the bottom column of the table reads "Accents: X--X-" the top should read "Beat:1 2 3 4 5" ... there should be between 1 and 12 columns generated (in addition to the labels 'Accents' and 'beats' on the left. –  Gregory Tippett Jun 6 '13 at 23:08
 
This answer always generates 7 columns –  Ziarno Jun 6 '13 at 23:10
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.