Jump to content

PHP search form error message


Buttero

Recommended Posts

I have made a search form for my website. When your search gets results a notification box comes up saying "The below articles contain the search term $search" This all works fine. But when there are no results for the search, I would like to stop this notification box from appearing, I have added a line of code to say "Sorry no results were found" but the notification box still appears. You can see this by going to seski.co.uk and searching some random letters. As you can see the box with the tick icon comes up, however there is a message below saying there was no results. What I want to do is make the notification box not appear when there is no results. And to display a different box with a different icon and font color etc.

Here is my code:

[code]<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","oo","oo");

//select which database you want to edit
mysql_select_db("oo");

$search=$_POST["search"];

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{
  //the format is $variable = $r["nameofmysqlcolumn"];
  //modify these to match your mysql table columns
 
  $title=$r["title"];
  $description=$r["description"];
  $user=$r["user"];
  $date=$r["date"];
  $id=$r["id"];
 
  //display the row
echo "<h2>".$title."</h2>";
echo "<h3>".$date." by ".$user."</h3>";

//explode $description into array and remove search keyword
$exp_desc = explode($search, $description);

//get number of parts to array
$num = count($exp_desc);

//loop through array
for($x=0;$x<$num;$x++) {

//display text from array
echo $exp_desc[$x];

//check if it is last entry in array
if($x+1 != $num) {

//display formatted search keyword
echo "<b>".$search."</b>";

}

}

echo "<hr>";
}
if(mysql_num_rows($result) == o) echo "Sorry, no results were found."
?>
[/code]

Thanks in advance
Link to comment
https://forums.phpfreaks.com/topic/29436-php-search-form-error-message/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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