Menu
is there a standard snippet of php or sql that I can place in my script that will tell searchers that there were no results to their query?
for instance… my db allows users to search for business of different categories located in different cities… when there are no particular businesses of the selected type for the city selected the script returns a blank page…
Can someone please show me some snippets that will ad something like :
“sorry, there were no results please try another location” etc. ?
Thanks
[code=php]if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}[/code]
[code=php]<?php [/code]
<?php [code=php]<?php
/*
broke this into functions...much easier to see and follow the flow of the program
*/
/* main code block */
//see if the form is submitted
if (!$_POST['submit']){
show_form();
}else{
//form submitted and need to get record(s)
get_data();
}//end if
//end main code block
function show_form()
{
//assume that city and category come from database
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
//get data for city and category
$sql = 'SELECT DISTINCT city FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$city[] = $row->city;
}
$sql = 'SELECT DISTINCT category FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$category[] = $row->category;
}
$result_city = mysql_query($sql) or die ("Can't connect to perform query one because ".mysql_error());
$result_category = mysql_query($sql) or die ("Can't connect to perform query two because ".mysql_error());
if ((!result_city)||(!$result_category)){
echo "error retrieving data";
}else{
//place the results into an array
while ($rows=mysql_fetch_array($result_city )){
$city[]=$rows['city'];
}
//while ($rows1=mysql_fetch_array($result_category)){
//$category[]=$rows1['category'];
//}
//show the form headers
echo "
<body>
<form action="" . $_SERVER['PHP_SELF'] . "" method="post">
<table>
<tr>
<td>Select city</td></td>
<td>Select category</td>
</tr>
<tr>
<td><select name="city">";
//show the city data
for ($x=0;$x<count($city);$x++){
echo "<option value="".$city[$x]."">".$city[$x];
}
echo "</select></td><td>
<select name="category">";
//add the category data
for ($x=0;$x<count($category);$x++){
echo "<option value="".$category[$x]."">".$category[$x];
}
echo "</select></td></tr></table>
<input type="submit" name="submit" value="search">
</form></body>";
}
}//end function show_form
function get_data()
{
//get data from form
$city=$_POST['city'];
$category=$_POST['category'];
$sql="select * from bus_list where city='$city' and category='$category'";
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}
// if the result didn't work, kill the script with an error
die($error_msg);
// count the result rows, do something with the value
$mysql_num_rows = mysql_num_rows($result);
switch($mysql_num_rows)
{
case 0:
die($no_results);
break;
case 1:
$results_str = "Search returned 1 result from the database:<br>n";
break;
default:
$results_str = "Search returned $mysql_num_rows results from the database:<br>n";
}
// if there is a result, print it out in a table
echo "<div align="center">n";
echo $results_str;
echo "<table border=1 cellpadding=2 cellspacing=0 width="90%">n";
echo "<tr>n";
echo "<td class='title'>name</td>n";
echo "<td class='title'>address</td>n";
echo "<td class='title'>city</td>n";
echo "<td class='title'>zip</td>n";
echo "<td class='title'>phone</td>n";
echo "<td class='title'>www</td>n";
echo "</tr>n";
while($row = @mysql_fetch_array($result))
{
extract($row); // grabs the array and splits into vars
echo "<tr>n";
echo "<td>$name</td>n";
echo "<td>$address</td>n";
echo "<td>$city</td>n";
echo "<td>$zip</td>n";
echo "<td>$phone</td>n";
echo "<td>$www</a></td>n";
echo "</tr>n";
}
echo "</table>n";
echo "</div>n<br><br>n";
echo $input_form."<br>n";
//} // close the second if-else statement
//else {
// no submission, print the regular form
echo $input_form;
} // close if statment for No RESULT
// close the first if-else statement
?> [/code]
[code=php]//get data from form
$city = $_POST['city'];
$category=$_POST['category'];
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
$sql="SELECT * FROM bus_list
WHERE city
='$city' AND category
='$category'";
$results = mysql_query($sql);
if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}[/code]
[code=php] if (!$result = @mysql_query($sql))
// if the result didn't work, kill the script with an error
die($error_msg);
// count the result rows, do something with the value
$mysql_num_rows = mysql_num_rows($result);
switch($mysql_num_rows)
{
case 0:
echo $no_results = "sorry no results:<br>n";
break;
case 1:
$results_str = "Search returned 1 result from the database:<br>n";
break;
default:
$results_str = "Search returned $mysql_num_rows results from the database:<br>n";[/code]
bus_list
name
address
city
zip
phone
www
notes
cat
id
subcatid
id
0.1.9 — BETA 5.16