Forum Moderators: coopster
Here is my code so far:
<?php do { ?>
<table width="100%" border="0">
<tr>
<td height=133 width=160 align=center>
<a href="details.php?prodID=<?php echo $row_rsResults['productID']; ?>&vendID=<?php echo $row_rsResults['vendorID']; ?>">
<img border=2 bordercolor="#000000" src="<?php echo $row_rsResults['image']; ?>" height=133 />
</a>
<br />
<span class="style6"><?php echo $row_rsResults['productName']; ?></span></td>
<td height=133 width=160 align=center>
<a href="details.php?prodID=<?php echo $row_rsResults['productID']; ?>&vendID=<?php echo $row_rsResults['vendorID']; ?>">
<img border=2 bordercolor="#000000" src="<?php echo $row_rsResults['image']; ?>" height=133 />
</a>
<span class="style6"><?php echo $row_rsResults['productName']; ?></span></td>
</tr>
</table>
<?php } while ($row_rsResults = mysql_fetch_assoc($rsResults)); ?>
Thanks for your help!
Table output is not that scary.
Create a counter. Set it to zero.
if counter is zero, output a <tr>.
Output a table cell.
Increment counter.
if counter >= 3, output a </tr>, reset counter to zero.
Restart loop.
Where people get lost is the last row; let's say you only have one or two items in the last row. This causes the cells to misalign and in some browsers, the layout can explode on you. So after your while/for/do loop, you do a small for each:
if counter > 0 and counter < 3
for i counter to 3 ( output cells )
output closing <tr>
output closing <table>
The previous lays out the logic, see the last post in this thread [webmasterworld.com] for a working example. The only difference is he/she is asking for five columns, and you would swap my hard-coded for each loop for the while loop of your mysql query.
<?php
function display_table($data=array('No data available'), $cols="3", $class="class"){
// add extra blank cells if needed
$needed = $cols - (count($data)%$cols);
if($needed == $cols) $needed = 0;
for($i=0;$i<$needed;$i++){
$data[] = ' ';
}
$data_chunks = array_chunk($data,$cols);
$display = "<table class=\"$class\">\r\n"; // initialize display
foreach($data_chunks as $chunk){
$display .= "<tr>\r\n";
foreach($chunk as $td){
$display .= "<td>$td</td>\r\n";
}
$display .= "</tr>\r\n";
}
$display .= "</table>\r\n";
return $display;
}
?>
So just populate an array with the info you need and then call the function like this:
echo display_table($array,3,"tableclass");