Page 1 of 1

Help making this Php function more efficient

Posted: Mon Mar 03, 2014 6:08 am
by chauhanRohit
Hi every one i wnat to improve the output quality of this function.

Code: Select all

if (isset($_POST['searchVal']) && trim($_POST['searchVal'])!='' && strlen('searchVal') > 3){

	$searchq = $_POST['searchVal'];
	$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
//query	
	$query = mysql_query("SELECT  * FROM authors WHERE name LIKE '%$searchq%'") or die("could not search");
	$count = mysql_num_rows($query);
	if($count == 0){
         $output = 'there was no search result!';
        }else{
        $output = '<ul ="dropdown">';
            while($row = mysql_fetch_array($query)){
            
              
                $output .= '<a class="searchresult" href="#"><li> '.$row['name'].'</li></a>';
           }
        $output .= '</ul>';
       }
    }
echo($output);
1. how do i make the function work when the string length is greater than three, i tried using strlen()
also any other thing that will make this better.

thanks.

Re: Help making this Php function more efficient

Posted: Mon Mar 03, 2014 10:16 pm
by Christopher

Code: Select all

if (isset($_POST['searchVal']) && trim($_POST['searchVal'])!='' && strlen($_POST['searchVal']) > 3){

Re: Help making this Php function more efficient

Posted: Sat Mar 08, 2014 2:13 am
by chauhanRohit
Christopher wrote:

Code: Select all

if (isset($_POST['searchVal']) && trim($_POST['searchVal'])!='' && strlen($_POST['searchVal']) > 3){
Sorry it works fine , i was having problem with the database.

thanks .

there is still one more problem that i am facing, how do i reset the search bar if i click outside the search bar?

Re: Help making this Php function more efficient

Posted: Sat Mar 08, 2014 6:45 am
by Celauran
How is it not working? What is the expected behaviour? What is actually happening? On what input?