Jump to content

[SOLVED] while, if and else issues


soddengecko

Recommended Posts

hi all.

 

I have a while statement that pulls all data from the db where a certain criteria is met. in this case where the manufacturer name matches the POST'ed value. works a treat.

 

here is the code:

 

while ($row = mysql_fetch_array( $result )) {
$row_class = ($row_count % 2) ? $class1 : $class2; 

  $id = $row['id'];
  $manu = $row['manufacturer'];
  $mod = $row['model'];
  $colour = $row['colour'];
  $whole = $row['wholesale_price'];
  $retail = $row['retail_price'];
  $quan = $row['quantity'];
  
  ?>
  
do html stuff here

<?
// Add 1 to the row count
$row_count++;
}

 

I have a set of links listing all the manufacturers in the db from the manufacturers table which is pre-populated with all manufacturers. when the link is clicked a page showing the DISTINCT models made by the defined manufacturer is shown. this all works inside my while loop.

 

the issue is when i try to create an else clause for products that are not in the db. so if i click on LG and there are no LG products i want an error message stating there is nothing to display. when I wrapped my while statement inside an IF it did this but only shows one product instead of looping through and displaying all.

 

this is the IF statement

 

<?
if ($row = mysql_fetch_array( $result )) {
$row_class = ($row_count % 2) ? $class1 : $class2; 

  $id = $row['id'];
  $manu = $row['manufacturer'];
  $mod = $row['model'];
  $colour = $row['colour'];
  $whole = $row['wholesale_price'];
  $retail = $row['retail_price'];
  $quan = $row['quantity'];
  
  ?>
  
do html here

<?
// Add 1 to the row count
$row_count++;
}
else {
echo "nada";
}
?>

 

how do i get the IF to loop through all data, or how do i nest a while statement inside the IF?

Link to comment
Share on other sites

This should do it

 

<?php

if (mysql_num_rows($result) > 0) {
    
    while ($row = mysql_fetch_array($result )) {
        $row_class = ($row_count % 2) ? $class1 : $class2;
        
        $id = $row['id'];
        $manu = $row['manufacturer'];
        $mod = $row['model'];
        $colour = $row['colour'];
        $whole = $row['wholesale_price'];
        $retail = $row['retail_price'];
        $quan = $row['quantity'];
        
        ?>
        
        do html stuff here
        
        <?php
        // Add 1 to the row count
        $row_count++;
    }
    
} else {
    echo "No Results";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.