for () {
--for () {
--}
}
The inner loop is executed in its entirety each time the outer loop runs once.
So, you get one of the outer loop (ie a single number) run through the inner loop fully-- outputting an entire row of that number.
You can't do this just with two loops. You'll need a bit more math than that. But nothing too complex.
Here's the simplest solution I can think of:
PHP Code:
$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
$cols = 5;
echo "<table border=\"5\" cellpadding=\"10\">";
for ($i=0; $i < count($input); $i++)
{
echo "<tr>";
for ($c=0; $c<$cols; $c++)
{
echo "<td>$input[$i+$c]</td>";
}
echo "</tr>";
$i += $c;
}
echo "</table>";
Note $i+$c.
There are a couple other ways as well, such as using $c%$cols, which would give the remainder of $c/$cols, which should work for you as the $c value, then just using $c value for the numbers. (Or replace $c with $i in the last sentence if that makes more sense-- you'd only have one.)
Also, though it doesn't apply so easily in this case, you should look into foreach() loops.
foreach($array as $item) {
echo $item;
}
foreach($array as $key => $item) {
echo $key.': '.$item;
}
By the way, using "$array[$item]" hasn't always worked for me, though that may be more the case with strings, like "$array['item']".
End the quotes, and use a dot to join the parts, and that will likely run smoother in at least some cases. I'm not sure about the specifics.
"something".$array[$item]."something";
Bookmarks