Angreal Posted May 11, 2009 Share Posted May 11, 2009 Hi folks Im new to this so please bear with me... Here is the working code for the page that I have so far: <?php // Make an SQL query to show all categories $CategorySQL = "SELECT * FROM nmc_category ORDER BY catDesc"; // connect to the database include('dbconnect.php'); // execute the query $rsCategory = mysql_query($CategorySQL); echo "<form action=\"DisplayMusic.php\" method=\"get\"> \n"; echo "<select name= \"CategoryName\">\n"; echo "<option value=\"all\">Show All CDs</option>"; // loop through all the categories in the record set while($row = mysql_fetch_array ($rsCategory)){ $CategoryDesc = $row['catDesc']; $CategoryID= $row['catID']; echo "\t<option value = \"$CategoryDesc\">$CategoryDesc</option>\n"; } // while // Each record display as an option in a form // end loop echo "</select>\n"; echo "<br /><input type=\"submit\" value=\"See Category\" />"; echo "</form>" ?> The results return a list of CDs from a database dependant on the category selected from a drop down menu. The results display on the next page (Displaymusic.php) and all this works fine and dandy One of the categorys has no results so I would like to have an error message display informing the user of this My problem is im not sure where to put the code or entirely sure what to write. tried to find an example but not ben able to find anything thats quite similar enough that I can fathom out... Any help is gratefully received!!! A Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/ Share on other sites More sharing options...
Angreal Posted May 11, 2009 Author Share Posted May 11, 2009 In addition I found the following: $result = @mysql_query($sql) or die("ERROR - Unable to select the database-".mysql_error()); $count = @mysql_num_rows($result); if($count > 0) { //fetch array or whatever } else { //Display "no results found" message } tried to integrate what I had into it but no joy so if anyone could give me a pointer for using the above that would be useful too Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831634 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Why not just not show that category? Then you wouldn't have to display any error messages. Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831635 Share on other sites More sharing options...
redarrow Posted May 11, 2009 Share Posted May 11, 2009 <?php if(isset($var['name']){ echo"do somethink"; }else{echo "there nothing to do";}?> Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831637 Share on other sites More sharing options...
Angreal Posted May 11, 2009 Author Share Posted May 11, 2009 Why not just not show that category? Then you wouldn't have to display any error messages. Its part of the criteria for a project. If there are no CDs in the category then a message reporting this needs to be displayed. @redarrow: ill try that and see what happens Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831643 Share on other sites More sharing options...
Angreal Posted May 11, 2009 Author Share Posted May 11, 2009 OK so now ive tried the following: if (mysql_num_rows($rsCategory) < 1) { echo "no results found"; } else { $row = mysql_fetch_array ($rsCategory); { $CategoryDesc = $row['catDesc']; $CategoryID= $row['catID']; echo "\t<option value = \"$CategoryDesc\">$CategoryDesc</option>\n"; } Does this look right? Im getting a unexpected $end error message too that is highlighting the </html> at the end of the page... Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831661 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 No. Remove the { after the $row line. Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831664 Share on other sites More sharing options...
Angreal Posted May 11, 2009 Author Share Posted May 11, 2009 ah right, silly mistake. Only problem now is that the rest of the categorys that were in the list have disappeard for some reason... Code now looks like this: <?php // Make an SQL query to show all categories $CategorySQL = "SELECT * FROM nmc_category ORDER BY catDesc"; // connect to the database include('dbconnect.php'); // execute the query $rsCategory = mysql_query($CategorySQL); echo "<form action=\"DisplayMusic.php\" method=\"get\"> \n"; echo "<select name= \"CategoryName\">\n"; echo "<option value=\"all\">Show All CDs</option>"; // loop through all the categories in the record set // while if (mysql_num_rows($rsCategory) < 1) { echo "no results found"; } else { $row = mysql_fetch_array ($rsCategory); $CategoryDesc = $row['catDesc']; $CategoryID= $row['catID']; echo "\t<option value = \"$CategoryDesc\">$CategoryDesc</option>\n"; } //Each record display as an option in a form // end loop echo "</select>\n"; echo "<br /><input type=\"submit\" value=\"See Category\" />"; echo "</form>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831678 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Try this - <?php // connect to the database include('dbconnect.php'); // Make an SQL query to show all categories $CategorySQL = "SELECT * FROM nmc_category ORDER BY catDesc"; // execute the query $rsCategory = mysql_query($CategorySQL); // loop through all the categories in the record set // while $out = ''; $errors = array(); while ($row = mysql_fetch_assoc($rsCategory)) { $CategoryDesc = $row['catDesc']; $CategoryID= $row['catID']; if (empty($CategoryDesc)) $errors[] = $CategoryID . ' is empty.'; else $out .= "\t<option value = \"$CategoryDesc\">$CategoryDesc</option>\n"; } if (!empty($errors)) { foreach ($errors as $error) { echo 'error: ' . $error . "\n"; } } echo "<form action=\"DisplayMusic.php\" method=\"get\"> \n", "<select name= \"CategoryName\">\n", "<option value=\"all\">Show All CDs</option>", $out, "</select>\n", "<br /><input type=\"submit\" value=\"See Category\" />", "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831685 Share on other sites More sharing options...
Angreal Posted May 11, 2009 Author Share Posted May 11, 2009 yeah thats kinda what im after. Ill have a play around with it tonight and what i can make of it. No sleep for me! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/#findComment-831696 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.