ok, there might be more efficient ways of doing this, but this will do what you want. if the last row is 3 cells, then it prints normal.
PHP Code:
<?
$array = array("a","b","c","d","e","f","g","h","i","j","k");
$count = count($array);
// assign the total full rows
$total_rows = floor($count/3);
// add 1 to total rows if there is one more incomplete row
if($count%3>0){$total_rows++;};
// set row counter to 0
$row_number = 0;
echo "<table border=\"1\" width=\"200\">\n";
foreach($array as $each)
{
// make a new if the remainder of $i divided by 3 == 0
if($i % 3 == 0)
{
// increment the row number variable
$row_number++;
echo "<tr>\n";
// setup last row only if row isn't 3 in length
if( $row_number == $total_rows && count($array)%3!=0)
{
echo " <td colspan=\"3\" align=\"center\">\n <table border=\"1\" width=\"100%\">\n <tr>\n";
}
}
// use the ternary operator to inject align tag if on the last row
echo " <td" . (($row_number==$total_rows && count($array)%3!=0)?" align=center":"") . ">$each</td>\n";
// end table row if the remainder of $i+1 divided by 3 is 0
if( (($i + 1) % 3 == 0) || ($row_number == $total_rows && ($i+1)==count($array) ))
{
// complete last row
// only close table if row isn't 3 cells
if($row_number == $total_rows && count($array)%3!=0)
{
echo " </tr>\n </table>\n</td>\n";
}
echo "</tr>\n";
}
$i++;
}
echo "</table>";
?>