SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php search engine

    hi there. Ive got a search engine that doesn't work. So far it's ment to say if there's any resilts found. However when i'm typing in the keywords it's saying no results found. could anyone please take a look??

    Code:
    <?php
    
    //get data
    $button = $_GET['submit'];
    $search = $_GET['search'];
    
    if (!$button)
    	echo "You didn't submit a keyword.";
    else
    {
    	if (strlen ($search)<=1)
    		echo "Search term too short.";
    	else
    	{
    		echo "You searched for <b>$search</b><hr size='1'>";
    	
    	//connect to database
    	 include('config.php');
    
    	 mysql_select_db("student_bay_co_", $connection);
    	
    	
    			
    			
    			//explode our search term
    			$search_exploded = explode(" ",$search);
    			
    			foreach($search_exploded as $search_each)
    			{
    				
    			//construct query
    				$x++;
    				if ($x==1)
    					$construct .= "keywords LIKE '%$search_each%'";
    				else
    					$construct .= " OR keywords LIKE '%$search_each%'";
    				
    			}
    			
    		
    		
    		//echo out construct
    		
    		$construct = "SELCECT * FROM products1 WHERE $construct";
    		$run = mysql_query($construct);
    		
    		$foundnum = mysql_num_rows($run) ;
    		
    		if ($foundnum==0)
    			echo "No results found.";
    		else
    		{
    			echo "echo $foundnum results found!<p>";
    			
    		}
    		
    	
    	}
    	
    }
    
    
    ?>

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's a v.quick overview...
    PHP Code:
    <?php
    $terms 
    = empty($_GET['search']) ? '' $_GET['search'] ;

    $sql "SELECT foo FROM table WHERE 1 <> 1";

    foreach(
    explode(' '$terms) as $term){
      
    $sql .= sprintf(" OR bar LIKE '%%%s%%'"mysql_real_escape_string($term));
    }

    $res mysql_query($sql);

    if(
    === mysql_num_rows($res)){
      
    #no results
    }else{
      
    #some results
    }
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi there thanx for the reply. i'm a beginner so i do not fully understand your code. could you explain please

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, I've added comments.

    PHP Code:
    <?php
    #check if search terms were sent, if not, set to ''
    $terms = empty($_GET['search']) ? '' $_GET['search'] ;

    #create a base sql query that negates all records by default
    $sql "SELECT foo FROM table WHERE 1 <> 1";

    #split the searchs terms by space char
    foreach(explode(' '$terms) as $term){
      
    #find a record where bar is 'like' the $term and add it to the base sql query
      
    $sql .= sprintf(" OR bar LIKE '%%%s%%'"mysql_real_escape_string($term));
    }

    #execute query
    $res mysql_query($sql);

    if(
    === mysql_num_rows($res)){
      
    #no results
    }else{
      
    #some results
    }
    If you're unsure of any of the functions used, see the manual; it's much better than me at explaining them.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •