Paul Wichy Paul Wichy - 1 year ago 320
HTML Question

table + foreach - new column?

<?php
$values = array();
for($i=0;$i<100;$i++){
$values[$i] = "aaa" . $i;
} ?>
<table>
<?php
foreach ($values as $i => $val) {

echo "<tr><td>" . $val . "</td> </tr>";
} ?>
</table>


this generated me:

aaa1
aaa2
...
aaa50
...
aaa90
...
aaa100


how can I make two column?

aaa1 aaa50
aaa2 ....
... aaa90
aaa50 aaa100


but no:

aaa1 aaa2
aaa3 aaa4
... ....
aaa99 aaa100

Answer Source

The way that I would do this is to create two separate tables (each one column wide) and then include both of them in a single, two-columned table:

<?php
$list=array('a','b','c','d','e','f');
$midpoint=floor(count($list)/2);
$tableHeader='<table width="100%">';
$tableFooter='</table>';
$leftTable=$tableHeader;
$rightTable=$tableHeader;
for ($c=0; $c<$midpoint; $c++)
{
    $leftTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$leftTable.=$tableFooter;
for ($c=$midpoint; $c<count($list); $c++)
{
    $rightTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$rightTable.=$tableFooter;
$mainTable='<table><tr><td width="50%">'.$leftTable.'</td><td width="50%">'.$rightTable.'</td></tr></table>';
echo $mainTable;
?>

Add some CSS to remove padding around the sub-tables and borders etc and you should be good to go.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download