how to add a if else statment in the search function
Posted: Fri Feb 21, 2014 12:11 am
hello every one, can any one help me with the searchbar.
i want to add a if else statment(or any other statement) so that if there is no search query , the search bar should not return anything.
currently the searchbar is returning the data even if there is no query(when the searchbar is empty and i press backspace
<?php
mysql_connect("localhost","root","") or die("could not connect");
mysql_select_db("test") or die("could not find database");
$output = '';
//collect
if(isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM authors WHERE fname LIKE '%$searchq%' OR lname LIKE '%$searchq%'") or die("could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'there was no search result!';
}else{
while($row = mysql_fetch_array($query)){
$firstname = $row['fname'];
$lastname = $row['lname'];
$output .= '<div> '.$firstname.' '.$lastname.' </div>';
}
}
}
echo($output);
?>
i want to add a if else statment(or any other statement) so that if there is no search query , the search bar should not return anything.
currently the searchbar is returning the data even if there is no query(when the searchbar is empty and i press backspace
<?php
mysql_connect("localhost","root","") or die("could not connect");
mysql_select_db("test") or die("could not find database");
$output = '';
//collect
if(isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM authors WHERE fname LIKE '%$searchq%' OR lname LIKE '%$searchq%'") or die("could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'there was no search result!';
}else{
while($row = mysql_fetch_array($query)){
$firstname = $row['fname'];
$lastname = $row['lname'];
$output .= '<div> '.$firstname.' '.$lastname.' </div>';
}
}
}
echo($output);
?>