1

I have an array which I would like to dump into a table, but have a new column X number of items. For example:

item 1 | item 2 | item 3 | item 4 |

item 5 | item 6 | item 7 | item 8 |

item 9 | and so on...

I can already make it add a new column after a certain number of items (in this case, 4) with this code:

$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); 

$cols = 4; 

echo "<table border=\"5\" cellpadding=\"10\">"; 

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    $n = $i+$c;
    echo "<td>$input[$n]</td>"; 
    } 
echo "</tr>"; 
$i += $c; 
} 

echo "</table>"; 

But for some reason, after one column ends with 'four', the next starts with 'six'.

1
  • It's because you're resetting $i within the loop, and then the for() itself will do $i++. Yuu should do either for($i...; $i += $c) at the top or $i += $c - 1; at the bottom Commented May 24, 2011 at 20:32

6 Answers 6

3

Array functions can be quite magical:

$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); 
$cols = 4; 
$table = array_chunk($input, $cols);

echo "<table border=\"5\" cellpadding=\"10\">";

foreach($table as $row) {
    echo "<tr>"; 
    foreach($row as $cell) {
        echo "<td>$cell</td>"; 
    }
    echo "</tr>"; 
}
echo "</table>";

ref: http://php.net/manual/en/function.array-chunk.php

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

3 Comments

I think $rows, $row, $cell will cause undefined variable message error.
@SIFE I think he meant $table instead of $rows. I changed it and it works fine now.
@Preston Yeah, I decided I didn't like $rows, but didn't do so thoroughly, thx for the edit.
1

On your first loop $i will be incremented by $c (which will always be 3). Then the for loop will increase the $i value by one ($i++) which will make it skip the 'five'.

You either control the increment or let the for loop control it for you.

Comments

0
$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); 

$cols = 4; 

echo "<table border=\"5\" cellpadding=\"10\">"; 

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    $n = $i+$c;
    echo "<td>$input[$n]</td>"; 
    } 
echo "</tr>"; 
$i += $c - 1; 
} 

echo "</table>";

Kai's answer is a much better solution. All I did was subtract 1 from the line $i += $c.

Comments

0

You're skipping every 5th because you are incrementing your counter an extra time with the for loop when you detect that you need to do a new row.

Here is another appraoch:

$ary = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8');


echo "<table><tr>";
foreach($ary as $k => $item)
{
    if($k % 4 == 0 && $k != 0)
        echo "</tr><tr>";
    echo "<td>$item</td>";
}
echo "</tr></table>";

Comments

0

If understand you clearly, you want to dump an array in number of rows specified by you:

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    echo "<td>$input[$i]</td>"; 
    } 
echo "</tr>"; 
} 

Comments

0

If you'd like to keep your original logic, change the increment of $i to $i += (c$-1) since you are incrementing $i in the loop in addition to your column width. Then also cater for empty values. This will work:

<?php
$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',     'ten'); 

$cols = 4; 

echo "<table border=\"5\" cellpadding=\"10\">" . PHP_EOL; 

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    $n = $i+$c;
    if ( $n < count($input))
        echo "<td>$input[$n]</td>" . PHP_EOL; 
    else
        echo "<td></td>" . PHP_EOL;
    } 
echo "</tr>"; 
$i += ($c-1); 
} 

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.