0

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.

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

your code prints a new row before printing the third cell; end of row checking should be after printing all cells not before; beside that you need to watch for first row opening tag <tr> and last row closing tag </tr>..
true, thanks. But as I said, it was more about demonstrating the use of modulo than delivering a functioning "copy&paste" solution. Still, you're right and I changed it. I left the code about opening and closing tags intentionally. There is also need to fill underful tr's if the array size is not a multiple of 3 and so on..
2

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

3 Comments

This may work but its not the optimal solution; array_chunk and two foreach loops is too expensive.. don't you think?!
Yeah right.. if you think that two loops are too expensive try doing three! Saleh, do some benchmarking before commenting. This wouldn't affect the web performance at all. There are hundreds of other things that screw up the performance and running a simple script like this isn't one of them. Besides he's obviously a newbie and my answer (I think) is the most obvious one.
No need for benchmarking $more_memory_allocation + $more_execution_tim = $too_expensive, but if you insist do it your self with a 1000 element array and 1000 visitor at the same time.
1

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

Comments

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

Comments

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

Comments

0

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>

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.