soddengecko Posted October 25, 2007 Share Posted October 25, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/74742-solved-while-if-and-else-issues/ Share on other sites More sharing options...
pocobueno1388 Posted October 25, 2007 Share Posted October 25, 2007 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74742-solved-while-if-and-else-issues/#findComment-377820 Share on other sites More sharing options...
soddengecko Posted October 25, 2007 Author Share Posted October 25, 2007 Absolutely perfect. Thank you very much for that one. That worked like a dream and is keeping my client happy. Quote Link to comment https://forums.phpfreaks.com/topic/74742-solved-while-if-and-else-issues/#findComment-377841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.