SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help needed exploding multi word search

    Hi

    i need help exploding a multi-word search.

    i found this code somewhere sometime ago which I run on the results page for this search form field below
    Code:
     <input name="newsearch" type="text" size="15">
    PHP Code:
    $newsearch addslashes(trim($newsearch)); 
        
    $search explode(" ",$newsearch); 
           
    //how many? 
           
    $count count($search); 
        echo 
    $count;
           
    //if there is just one word one statement no loop 
            
    if ($count=="1"
            {    
                
    $req $newsearch
            } 
            else 
            { 
            
    //loop the words 
              
    for($i=0;$i<$count;$i++) 
              { 
                  
    //loop the sql statement 
                  
    for ($j=$count;$j>$i;$j--) 
                  { 
                    
    //keep adding 'or' to the query 
                    
    if ($i<=$count-2
                    { 
                      
    $add=" or "
                    } 
                    
    //for the last loop don't use 'or' 
                    
    else 
                    { 
                    
    $add=""
                    } 
                   } 
                  
    //the complete query string 
     
    $req "$search[$i]$add";
                echo 
    $req;//LETS SEE WHAT WE HAVE THEN
                

            } 
    If I enter ex designer in the search field then
    PHP Code:
    echo $req;//LETS SEE WHAT WE HAVE THEN 
    duly outputs
    Code:
    ex or designer
    But when I echo my search query

    PHP Code:
    SELECT FROM mytable WHERE 1 AND public='Yes' AND (name like '%designer%' or description like '%designer%'order by sortorder ASC 
    you'll see it only searches on the LAST word of the search term. (IE DESIGNER)

    This is better than my previous effort which would not have returned anything for 'ex designer'. At least now I have something returned as long as the last word is relevant.

    But can someone help me make so that if ANY word is relevant I'll have some results returned?

    TIA

  2. #2
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guesss that you can use match instead "or" or is so slow
    http://dev.mysql.com/doc/refman/5.0/...xt-search.html
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  3. #3
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its because you're re-assigning the request everytime through the loop.

    Intead of

    $req =

    use

    $req .=

    This will append the new stuff onto whats already there rather then replace it.
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  4. #4
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    thanks guys.

    ruba- i'm sure you are right but that looka little off road for me at the moment. I tried
    Code:
    SELECT * FROM products WHERE 1 AND public='Yes' AND MATCH (name, description) AGAINST ('$req') order by sortorder ASC
    and couldn't get it to work.

    Jeanco

    Your suggestion worked in that it enabled me to bring in all the words - but you'll see there is something wrong in my syntax that mean it only cheks the first word....

    Code:
    SELECT * FROM products WHERE 1 AND public='Yes' AND (name like '%dummy%' or '%ex%' or description like '%dummy%' or '%ex%') order by sortorder ASC

    i need to find a way to create something like
    Code:
    SELECT * FROM products WHERE 1 AND public='Yes' AND (name like '%dummy%' or name like  '%ex%' or description like '%dummy%' or description like '%ex%') order by sortorder ASC
    apologies to ruba. this must look terrible!

  5. #5
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fulltext searching would probably be a better idea...look it up in the mysql manual.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom



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
  •