Hi all,
Been working on my search script, and it all works fine, apart from one thing. If I enter a keyword that is not found in the table I am searching, the results page displays nothing. I want the results page to display a custom error (i.e "No matches found, click here to search again")
The array displays results (if results found) correctly, and also displays a custom error for no keyword entered, but I cannot seem to find the corrct place to put the final else statement.
I'll include the code here, broken down into sections to make more sense:-
This is the opening array, which is fine:
if ($search) // perform search only if a string was entered.
{
mysql_connect("localhost","user","pass") or die ("Problem connecting to Database");
mysql_select_db("my_db") or die( "Unable to select database");
$srch="%".$search."%";
$query = "SELECT * from my_table WHERE date LIKE '$srch' || exhibition LIKE '$srch' || address LIKE '$srch' || time LIKE '$srch' || contact LIKE '$srch' || notes LIKE '$srch' ORDER by date ASC";
$result = mysql_db_query("my_db", $query)
or print ">>> MySQL-Error: ".mysql_errno()." -> ".mysql_error()."<br>\n";
Now for the array that matters - the result:
if ($result)
{
echo "<img src=\"$url/images/exhibits-main.jpg\"><br><br>";
echo "<strong>Exhibition Search Results, listed by date order:</strong><br><br><br>";
while ($r = mysql_fetch_array($result)) { // Begin while
$date = $r["date"];
$exhibition = $r["exhibition"];
$address = $r["address"];
$time = $r["time"];
$contact = $r["contact"];
$notes = $r["notes"];
echo "<table width=\"50%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" bordercolor=\"#000000\">";
echo "<tr>";
echo "<td class=\"exhibitdate\"><strong>$date</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>$exhibition</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td>$address</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Time:</strong> $time</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Contact:</strong> $contact</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$notes</td>";
echo "</tr>";
echo "<br><br>";
} // end while
echo "</table>";
echo "<br><br><br><br><br><br><br><br><br><br>";
}
Then I end with some else statements, and this is where I'm sure I need another else statement if there is no result, but cannot figure where to put it:
else { echo "The script is broken";
}
} else {
echo "You did not enter anything to search for.<br>Please <a href=\"search.php\">go back</a> and enter a search word.";
}
Now the last two else statements (in order) only come into effect if:
- The script's syntax is broken
- Nothing was entered into the search field
Does anyone know where I put the else (or perhaps elseif?) statement to allow for no results, which will return something like:
else { echo "No matches found, click here to search again";
}
...because no matter where I put that statement within the script, it either breaks the script or affects other else statements.
Help!!!
TIA
Jonathen