0

I am trying to use for loops to create a table which dynamically returns something like this: Note how the td content have been arranged

<table>
 <tr>
  <td>1</td>
  <td>3</td>
  <td>5</td>
  <td>7</td>
  <td>9</td>
 </tr>
 <tr>
  <td>2</td>
  <td>4</td>
  <td>6</td>
  <td>8</td>
  <td>10</td>
 </tr>
</table>

among What I have tried so far is

$row=2;
$col=20;
$x=0;
echo '<table>';
for($i=0;$i<$row;$i++){
   echo '<tr>';
     for($k=0;$k<$col;$k++)
     {
        echo '<td>'.echo $x+=1.'</td>';
     }
   echo '</tr>';

}

In this case I get something different and which is not what I want.

<table>
    <tr>
     <td>1</td>
     <td>2</td>
     <td>3</td>
     <td>4</td>
     <td>5</td>
    </tr>
    <tr>
     <td>6</td>
     <td>7</td>
     <td>8</td>
     <td>9</td>
     <td>10</td>
    </tr>
  </table>

Kindly someone help me map this.Thanks

4
  • Thanks for correction. Tried to format when posting hence the typo error.Sorry for that Commented May 21, 2012 at 3:05
  • Are you just wanting a table with numbers, or are the numbers an example of how you want the data sorted? ie are you chasing a piratical use of this loop? Commented May 21, 2012 at 3:07
  • I would like to display content in a table. They have index which will take that no format. In short my table numbers should be arrange in that manner Commented May 21, 2012 at 3:10
  • Is it that you know the number of columns you want or you know the number of rows you want? Commented May 21, 2012 at 3:11

4 Answers 4

1
<?php 
  # how high to count
  $count = 10;

  # how many rows in the table
  $rows = 2;

  # figure out how many columns
  $columns = ceil($count/$rows);

  # could also go the other way and declare there to be 5 columns and then 
  # divide to get the rows, but since you're counting down the columns, 
  # I thought this made more sense.  Either way.

?><table><?php 

  for ($i=0; $i<$rows; ++$i) {

 ?><tr><?php

    for ($j=0; $j<$columns; ++$j) {

      # calculate which number to show in column $j of row $i.  Each column adds
      # $rows numbers to the total, while each row just adds one more.  And we
      # want to start at 1 instead of 0.
      $n = $j * $rows + $i + 1;

      ?><td><?= $n ?></td><?php 
    }

  ?></tr><?php

  }
?></table>
Sign up to request clarification or add additional context in comments.

Comments

1
$total_count = 10;
$total_rows = 2;

$table = array();
//building table array
for($i=1;$i<=$total_count;$i++) {
    $row = $i % $total_rows;
    $row = $row == 0 ? $total_rows : $row;
    $table[$row][] = $i;
}

//generate table based on array
echo "<table>";
for($row=1;$row<=$total_rows;$row++) {
    echo "<tr>";
    foreach($table[$row] as $cell) {
        echo "<td>".$cell."</td>";
    }
    echo "</tr>";
}
echo "</table>";

1 Comment

No need for an array here. Its much simpler than this.
1

This isnt as complicated as people are making it seem

Start the inner loop at whatever row you're currently on and add 2 each time.

<?php
$rows=2;
$cols=10;
?>


<table>
<?php for($i=1;$i<=$rows;$i++): ?>
    <tr>
    <?php for($k=$i;$k<$cols;$k+=2): ?>
        <td><?php echo $k ?></td>
    <?php endfor; ?>
    </tr>
<?php endfor; ?>
</table>

Id probably use range and foreach though

<?php
$rows=2;
$cols=10;
?>

<table>
<?php foreach( range( 1, $rows ) as $row ): ?>
    <tr>
    <?php foreach( range( $row, $cols, 2 ) as $col ): ?>
        <td><?php echo $col ?></td>
    <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
</table>

1 Comment

It depends on what is known going in, Galen. If it's predetermined that there shall be 5 columns, great. But if it's only "I want to count to N in column-major order across two rows", then you have to do a little math. I think my solution is pretty darn simple.
0

This approach will split the data itself

function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
{
  if (is_array($array) == true and !empty($array))
  {
    $rows = ceil(count($array)/$cols);
    $array = array_chunk($array,$rows);
    if (count($array) < $cols)
    {
      $array = array_pad($array,$cols,$pad);
    }
    foreach($array as $key => $value)
    {
      $array[$key] = array_pad($value,$rows,null);
    }
    foreach($array as $key => $value)
    {
      foreach($value as $sub_key => $sub_value)
      {
        $output[$sub_key][$key] = $sub_value;
      }
    }
    return $output;
  }
  return $array;
}

$data = array(1,2,3,4,5,6,7,8,9,10,11);

echo '<table border="1">';
foreach(array_chunk_vertical($data,ceil(count($data)/2)) as $row)
{
  echo '<tr>';
  foreach($row as $col)
  {
    echo '<td>' . $col . '</td>';
  }
  echo '</tr>';
}
echo '</table>';

Comments

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.