Forum Moderators: coopster

Message Too Old, No Replies

new row <tr> after 5 cells <td> - how do i do this?

         

marcus76

1:58 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



Hi,

Can someone please help me out with this - i've this piece of code which basically lists out some products down the page (vertically) - i'd like to change it so it lists them out across the page (horizontally) but limit the number to 5 across before it goes to a new row.

$col = 0;
$pwidth = 25;
echo '<td>&nbsp;</td><td class="tableHeading" width="'.$pwidth.'%" valign="top" align="left">';
if ($n > 0) {
$col++;
echo '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action','lng','currency')) . 'action=cp'). '"><img src="images/erase.gif" border="0" alt="' . ERASE_MY_RECENT_HISTORY . _OF_PRODUCTS . '" title="' . ERASE_MY_RECENT_HISTORY . _OF_PRODUCTS . '"></a>&nbsp;'.VIEWED.PRODUCTS .'<br>' . tep_draw_separator('pixel_trans.gif', '100%', 4);
echo '<table cellpadding="2">';
for ($i=0; $i<$n; $i++) {
echo '<tr height="40px"><td valign="top" align="center">' . tep_image(DIR_WS_IMAGES . $items_on_display[$i]['image'],$items_on_display[$i]['name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
echo '</td><td class="main" valign="top">';
echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_on_display[$i]['id']) . '">' . $items_on_display[$i]['name'] . '</a>';
echo '</td></tr>';
}
echo '</table>';
}
echo '</td>';

thanks

Marcus

d40sithui

3:00 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



hello,
just put a counter variable in the loop and echo "</tr><tr>" when the condition is right. in this example, i use modulus to make it simpler.

$counter = 0;
echo "<table>";
for($i = 0; $i < $n; $i++){
if($counter%5==0){
echo "<tr>";
}
echo "<td>DISPLAY INFO HERE</td>";
$counter++;
if($counter%5==0){
echo "</tr>";
}
}
if($counter%5!=0){
echo "</tr>";
}
echo "</table>

panos

3:14 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



So you want to display 5 products/row:

row_products=5;

define another variable before the for loop:

displayed_products=0;

in your loop:

if displayed_products==row_products then declare displayed_products=0 and change row
else stay in the same row and increment displayed_products by one:

displayed_products++;
end for loop

That's the basic idea, you will have to add some spaces if your products are not a multiple of row_products

marcus76

3:18 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



thanks both for your suggestions, will give it a go this evening..

;-)