Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Repeating Table

I can make a repeating list, but how to get it into a table?

         

Shay_Grey

7:49 pm on Apr 24, 2009 (gmt 0)

10+ Year Member



It has been working fine as a list. Now I am trying to make it into a 3 column table and I don't know what I am doing. Any help would be appreciated!

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!

rocknbil

2:10 am on Apr 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Shay_Gray, are you coming to PHP from ASP? Just curious, I have a reason for asking. :-)

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.

Shay_Grey

9:28 pm on Jul 7, 2009 (gmt 0)

10+ Year Member



I still can't get this to work. It is really frustrating me. Any help would be appreciated.

mooger35

10:47 pm on Jul 7, 2009 (gmt 0)

10+ Year Member



What I've did recently was build a function that basically did this for me as it's a pretty common need.

<?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[] = '&nbsp;';
}

$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");