SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Simple search ?

  1. #1
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple search ?

    Hi all,

    I've written a simple search page and all seems to be working fine.

    However a frind of mine asked why didnt I make it accpet more than 1 keyword

    Ive seen alot of sites around that allow multiple keyword searches by seperating them using commas.

    Can someone shed a little light on this matter please.

    Heres my simple output for a single keyword search:

    PHP Code:
    $search=$_POST["search"];
        
        if(
    get_magic_quotes_gpc()){ 
        
    $search stripslashes($search); 
        }
        
    $search mysql_real_escape_string($search); 

        
    $result mysql_query("SELECT * FROM ".$tablelinkname.
        WHERE 
        question LIKE '%
    $search%'
        AND
        answer LIKE '%
    $search%'");

        if( 
    mysql_num_rows($result) > 
        { 
        
        while(
    $r=mysql_fetch_array($result)) 
        { 
        
    $question=$r["question"];   
        
    $id=$r["id"];
        
    $cid=$r["cid"];
             
        echo 
    "    <a class='content_padding' href='read.php?cid=".$r['id']."'>".$r['question']."</a>"
        } 
        echo 

        "

        } 
        else 
        { 

          echo 
    "<p class='content_padding'><img src='img/caution.gif' align='top'/> Your search returned no results, 
        try using different key words.</p>"
    ;
        } 
        
    ?> 
    Regards
    Deano

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You'll need to split the search string into its component words using explode(), then dynamically make your query with each of the values in the array.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at this article, it's an excellent script for doing full text searches. Basically, it splits the string at ever space and does a mysql search comparing with regular expressions.

  4. #4
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx Akash, Ill have a read of it later today.
    Regards
    Deano


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
  •