Help making this Php function more efficient

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chauhanRohit
Forum Newbie
Posts: 7
Joined: Thu Feb 20, 2014 11:28 pm

Help making this Php function more efficient

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help making this Php function more efficient

Post by Christopher »

Code: Select all

if (isset($_POST['searchVal']) && trim($_POST['searchVal'])!='' && strlen($_POST['searchVal']) > 3){
(#10850)
chauhanRohit
Forum Newbie
Posts: 7
Joined: Thu Feb 20, 2014 11:28 pm

Re: Help making this Php function more efficient

Post 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?
Last edited by chauhanRohit on Mon Mar 10, 2014 5:33 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help making this Php function more efficient

Post by Celauran »

How is it not working? What is the expected behaviour? What is actually happening? On what input?
Post Reply