Jeg legede lidt med det, da jeg før har lavet både en vertikal og horisontal - jeg blev lidt træt af at have to seperate koder, så skrev dem sammen :)
Du kan se resultatet her :
http://ljweb.dyndns.org/test/formatarray.phpOg se min frygtelige slamkode (mangler optimering) her :
<?php
# Array Table formatter
# By: Lars B. Jensen (lars.jensen@ljweb.com)
function prepareArray($array, $rows=3, $direction='horisontal', $void=' ') {
$res = array();
$ressize = sizeof($array);
if ($direction == 'horisontal') {
$row = -1;
for ($i=0; $i<$ressize; $i++) {
if ($i%$rows == 0) $res[++$row] = array();
$res[$row][$i%$rows] = $array[$i];
}
} else {
$row = 0;
$length = ceil($ressize/$rows);
for ($i=0; $i<$ressize; $i++) $res[$i%$length][] = $array[$i];
}
for ($i=0; $i<sizeof($res); $i++) $res[$i] = array_pad($res[$i], $rows, $void);
return $res;
}
function formatTable($content, $width=300, $border=0, $cellspacing=0, $cellpadding=0, $align='left') {
$tdwidth = floor(100/sizeof($content[0]));
$res = '<table width="'.$width.'" border="'.$border.'" cellspacing="'.$cellspacing.'" cellpadding="'.$cellpadding.'" align="'.$align.'">';
for ($i=0; $i<sizeof($content); $i++) {
$res .= "\n<tr>";
for ($j=0; $j<sizeof($content[$i]); $j++) {
$res .= '<td width="'.$tdwidth.'%">'.$content[$i][$j].'</td>';
}
$res .= "\n</tr>";
}
$res .= "\n</table>";
return $res;
}
?>
<b>Horisontal</b><br>
<?php
# Foo array
$foo = array(1,2,3,4,5,6,7,8,9,10);
$formatarray = prepareArray($foo, 4, 'horisontal');
echo formatTable($formatarray, 200, 1);
?>
<br clear="all"><br>
<b>Vertikal</b><br>
<?php
# Foo array
$foo = array(1,2,3,4,5,6,7,8,9,10);
$formatarray = prepareArray($foo, 4, 'vertical');
echo formatTable($formatarray, 200, 1);
?>