Forum Moderators: coopster

Message Too Old, No Replies

While loop with if statement

While loop with if statement

         

ryrocks

10:06 am on May 21, 2008 (gmt 0)

10+ Year Member



Hi,
I am trying to display items from my database with a display LIMIT of 2. This works fine. But if there are no items in the database matching my query i want a message displayed saying "sorry no items available..."

This is where i've got to:

$query ="SELECT * FROM promo WHERE catalogue = '$cat' AND offer_type = 'new products' ORDER BY created DESC LIMIT 2";
$result = mysql_db_query ($database, $query);

if($row = mysql_fetch_array($result)){

echo "<div class=\"cats_newprods_1\">";

echo "<img class=\"cat_prod_img\" src=\" ";
echo $row['main_image_thumb'];
echo " \" \>";

echo "<h2 class=\"other_cat_header\">";
echo $row['page_title'];
echo "</h2>";

echo "<p class=\"pagetext\">";
echo $row['pg_description'];
echo "</p>";

echo "</div>";
}
else {
echo "<p class=\"pagetext\">There are currently no new products available from the Fisher Scientific $cat catalogue at this time</p>";
}

This works fine apart from it only displays ONE item as it is not a WHILE loop. How can i turn this into a while loop with the relevant IF statement?

Many thanks in advance.
Ryan

wheelie34

10:31 am on May 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try adding this under your $result = mysql_db_query line

$num_rows = mysql_num_rows($result);
if ($num_rows < 1)
{
echo "sorry no items available";
}
else
{
# run your while loop here
}

HTH
edit: forgot to say, welcome to webmasterworld Ryan

ryrocks

11:44 am on May 21, 2008 (gmt 0)

10+ Year Member



aha! of course! Thanks wheelie that's done the trick

Cheers,
Ryan