I have an array which I would like to dump into a t开发者_开发百科able, 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'.
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
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.
$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
.
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>";
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>";
}
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>";
?>
精彩评论