SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create an error message if a blank table is returned as a result

    Hey everyone!

    Having a few issues with a project im working on.

    Im trying to get an error message to return when the site im working on returns a table with no results.
    Its a CD sales website and the user can sort music by different categories. This works fine. However, if they select a category that has no CDs in it id like to return an error message rather than just a blank table...(removing the category from the list isnt an option.

    Ive tried something like:

    Code:
    <?php
    // Make an SQL query to show all categories
    $CategorySQL = "SELECT * FROM nmc_category ORDER BY catDesc";
    
    // connect to the database
    include('*file URL*');
    
    // execute the query
    $rsCategory = mysql_query($CategorySQL);
    echo "<form action=\"*FILE URL*\" 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>"
    ?>
    though im just getting errors back...if anyone can point me in the right direction i shall be very grateful!

  2. #2
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also: using the above code I dont get the list of categories inthe drop down menu and im not sure why either.

    stuff in ** is because im not worthy of posting URLs yet!

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sheesh. Just google it dammit. thats not a hard one to solve.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by unixcommerce View Post
    sheesh. Just google it dammit. thats not a hard one to solve.
    This may be something very simple to you, but obviously this is something the OP is having trouble with. Last I checked the whole purpose of this forum and others is to help / guide people. It is not here to make someone feel like they are being a bother. If that is how you feel, that is fine. Move on to another post if you don't have something positive to respond with.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are not seeing the list of categories because you are missing the while condition. To display just the error if there is no results and no form, you can move the form code into the else condition.

    PHP Code:
    // Make an SQL query to show all categories
    $CategorySQL "SELECT * FROM nmc_category ORDER BY catDesc";

    // connect to the database
    include('*file URL*');

    // execute the query
    $rsCategory mysql_query($CategorySQL);

    if (
    mysql_num_rows($rsCategory) < 1) {
      echo 
    "no results found";
    } else {

    echo 
    "<form action=\"*FILE URL*\" method=\"get\"> \n";
    echo 
    "<select name= \"CategoryName\">\n";
    echo 
    "<option value=\"all\">Show All CDs</option>";
      while (
    $row mysql_fetch_assoc ($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>"


  6. #6
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ unixcommerce: as a beginner I found it quite hard to solve so thanks for your input...

    Anyway @ Spiderling: thanks for the code. ive used that as a basis and adapted it a little bit. ive changed it a little so the check for the number of rows is done on the page with the results on rather than before the page is displayed.

    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •