4

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?

2 Answers 2

2

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>
5
  • 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.
    – drenl
    Commented Jun 6, 2013 at 22:56
  • $beats is rand(0,11) so table can have different number of columns - sometimes 7, sometimes more, sometimes less.
    – furas
    Commented Jun 6, 2013 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.
    – drenl
    Commented Jun 6, 2013 at 23:08
  • This answer always generates 7 columns
    – Ziarno
    Commented Jun 6, 2013 at 23:10
  • Now it generates a random number of beats.
    – Logan
    Commented Jun 6, 2013 at 23:15
1
<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>
3
  • 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.
    – drenl
    Commented Jun 6, 2013 at 23:15
  • Right, I forgot about the "Accent" label at the start of the second row - now it should be fine.
    – Ziarno
    Commented Jun 6, 2013 at 23:17
  • Ziarno, you are a true renaissance man or woman. I shall sing your praises to serialism geeks far and wide!
    – drenl
    Commented Jun 6, 2013 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.