mauzilla
mauzilla

Reputation: 3592

Dynamically create HTML table rows based on php array

I am seriously struggling to grasp my head around the following. I want to build a 3 data cell per row table based on an PHP array. So in other words, if there is 3 values in the array, there should be a structure like:

<?php
$arr = array("value1","value2","value3");
?>

// Expected outcome:
<table>
      <tr>
           <td>value1</td>
           <td>value2</td>
           <td>value3</td>               
      </tr>
</table>

but should a 4th value be added to the array, it must dynamically create another row so in other words:

<?php
$arr = array("value1","value2","value3","value4");
?>

// Expected outcome:
<table>
      <tr>
           <td>value1</td>
           <td>value2</td>
           <td>value3</td>               
      </tr>
      <tr>
           <td>value4</td>
           <td></td>
           <td></td>               
      </tr>
</table>

I really don't mind which solution, even a mix between php and jQuery, but just something I can use to achieve the above.

Upvotes: 0

Views: 7515

Answers (6)

Dario Pedol
Dario Pedol

Reputation: 2110

Use modulo. Like so:

<table>
<tr>
<?php
    $i = 1;
    foreach ($arr as $val){
        $i++;
        print '<td>'.$i.'</td>';
        if ($i % 3 == 0){
            print '</tr><tr>'^;
        }

    }
?>
</tr>
</table>

You will need to add some more stuff for correct html output, but the "hard" part is done.

Don't just copy and paste, I didn't test the code and it's ugly.

Upvotes: 5

elo
elo

Reputation: 615

Here is a logic implementation :

<?php
$input_array = array('a', 'b', 'c', 'd', 'e','f','g');
$new_array = array_chunk($input_array, 3);

$table = '<table border="1">';
foreach($new_array as $value){
$table .= '<tr><td>'.$value[0].'</td><td>'.$value[1].'</td><td>'.$value[2].'</td>    </tr>';
}
$table.='</table>';

echo $table;
?>

Upvotes: 1

Saleh Ghaleb
Saleh Ghaleb

Reputation: 156

Here is my suggestion which will produce will formatted html

<table>
    <tr>    
    <?php
    $i = 0;
    $items_per_row = 3;

    foreach ($arr as $elm) {
        echo '<td>'.$elm.'</td>';

        if (++$i % $items_per_row == 0 && $i < count($arr) - 1)
            echo '</tr><tr>';
    }
    ?>
    </tr>
</table>

Upvotes: 0

LHolleman
LHolleman

Reputation: 2596

<table>
    <tr>
        <?php
        $x = 0;
        foreach($arr as $v){
            if ($x % 3 == 0 && $x != 0){
                echo '</tr><tr>';
            }

            echo '<td>'.$v.'</td>';
            $x++;
        }
        ?>
    </tr>
</table>

Upvotes: 0

MireSVK
MireSVK

Reputation: 31

<table><tr>
<?php
$arr = array("value1","value2","value3","value4","value5","value6","value7");

for($i=0;$i<count($arr)%3;$i++)
  $arr[] = null;

foreach($arr as $key => $val){

  if(($key)%3==0)
    echo '</tr><tr>';

  echo '<td>'.$val.'</td>';

}
?>
</tr></table>

Upvotes: 0

Ignas
Ignas

Reputation: 1965

Use array_chunk function to split the array into groups and then just do a couple of loops e.g.

<?php
$arr = array("value1","value2","value3","value4");
echo "<table>";
$rows = array_chunk($arr,3);
foreach($rows as $row) {
  echo "<tr>";
  foreach($row as $cell) {
    echo "<td>".$cell."</td>";
  }
  echo "</tr>";
}
echo "</table>";
?>

Upvotes: 2

Related Questions