Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/157697-error-message-if-a-table-is-blank/
Share on other sites

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

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

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...

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>"
?>

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>";
?>

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.