Modifying the code from post #3 wasn't difficult to make it a table layout... so here it is:
<?php
function vertHorizLoop($array, $cols=3, $horiz=true, $table=true)
{
$output = '';
// Define some defaults
$count = count($array);
if($table)
$output .= '
<table>
<tr>';
if(!$horiz)
{
$sect = array();
for($i=0; $i<$cols; $i++)
$sect[$i] = array();
$max = ceil($count/$cols);
$k=0;
for($i=0; $i<$count; $i++)
{
if($i%$max==0 && $i!=0)
$k++;
$sect[$k][] = $array[$i];
}
}
if($horiz)
{
for($i=0; $i<count($array); $i++)
{
if($i%$cols==0)
{
if($table)
$output .= '
</tr>
<tr>';
else
$output .= '<br>';
}
if($table)
$output .= '
<td>'.$array[$i].'</td>';
else
$output .= $array[$i].' ';
}
}
else
{
for($i=0; $i<$max; $i++)
{
foreach($sect as $key=>$arr)
{
if($table)
$output .= '
<td>'.$arr[$i].'</td>';
else
$output .= $arr[$i].' ';
}
if($table)
$output .= '
</tr>
<tr>';
else
$output .= '<br>';
}
}
if($table)
$output .= '
</tr>
</table>';
echo $output;
}
// Numbers for the 3x4 block:
$block = array('1','2','3','4','5','6','7','8','9','10','11','12');
// call our function:
echo '<h3>Horizontal [<i>Table</i>]:</h3>';
vertHorizLoop($block, 3, true); // Horizontal
echo '<hr>
<h3>Vertical [<i>Table</i>]:</h3>';
vertHorizLoop($block, 3, false); // Vertical
echo '<hr>
<h3>Horizontal [<i>HTML</i>]:</h3>';
vertHorizLoop($block, 3, true, false); // Horizontal
echo '<hr>
<h3>Vertical [<i>HTML</i>]:</h3>';
vertHorizLoop($block, 3, false, false); // Vertical
?>
That outputs:
Horizontal [Table]:
1 2 3
4 5 6
7 8 9
10 11 12
Vertical [Table]:
1 5 9
2 6 10
3 7 11
4 8 12
Horizontal [code=html]:
1 2 3
4 5 6
7 8 9
10 11 12
Vertical [code=html]:
1 5 9
2 6 10
3 7 11
4 8 12
KEYWORDS:
Vertical / Horizontal Loop
Horizontal / Vertical Loop
Vertical Column
Vertical table listing