...

View Full Version : 3 Items per row or less IF truble



Lee Stevens
11-18-2007, 08:46 PM
Hi im writing a script wich i would like to display 3 pruducts in a row.

The code below works if there is 3 items per row. but i still need it to end the div even if theres only 1 or 2



$i = 0;

while ($row = mysql_fetch_array($result))
{
extract($row);

if ($i % $itemsPerRow == 0) {
$prod .= '<div id="modules">'; // Start main class
}

$prod .= '<div id="mod'. $i .'">' . // sub class
'<div class="padding">' .
'<div class="text">' .
'<b><a href="#" title=""></a></b>' .
'<p></p>' .
'</div>' .
'</div>' .
'</div>';
$i += 1;

if ($i % $itemsPerRow == 0)
{
$i = 0;
$prod .= '</div>'; // Close class full row or not
}
}

Majoracle
11-18-2007, 11:31 PM
// get total items (edit as needed)
$get_total = mysql_query("SELECT count(*) AS total FROM table_name");
$totalf = mysql_fetch_array($get_total);
$total = $totalf['total'];

$i = 0;
$n = 1;

while ($row = mysql_fetch_array($result))
{
extract($row);

if ($i % $itemsPerRow == 0) {
$prod .= '<div id="modules">'; // Start main class
}

$prod .= '<div id="mod'. $i .'">' . // sub class
'<div class="padding">' .
'<div class="text">' .
'<b><a href="#" title=""></a></b>' .
'<p></p>' .
'</div>' .
'</div>' .
'</div>';
$i += 1;

// close if is third item in row or last item
if ($i % $itemsPerRow == 0 OR $n == $total)
{
$i = 0;
$prod .= '</div>'; // Close class full row or not
}

$n++;
}

I THINK that'll solve it. It's how I used to do it, but when I do thing's like this now, I just use floating DIVs. Anyway, hope this works for you though.

Lee Stevens
11-19-2007, 12:43 AM
Thank you for your reply, i finaly fixed it a added a for clause



for ($i = 0; $i < $numItem; $i++)
{
// code inside here fideled a lil bit.
}


So funny how something can seem so easy but somtimes take so long



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum