felix cheruiyot
felix cheruiyot

Reputation: 311

PHP AND FOR LOOP

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

Upvotes: 0

Views: 371

Answers (4)

Galen
Galen

Reputation: 30170

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>

Upvotes: 1

Scuzzy
Scuzzy

Reputation: 12322

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>';

Upvotes: 0

ephemeron
ephemeron

Reputation: 396

$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>";

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95242

<?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>

Upvotes: 1

Related Questions