problem in advance search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amel86
    New Member
    • Apr 2010
    • 25

    problem in advance search

    I have search code that is below:

    Code:
    $term = $vbulletin->input->clean_gpc('r', 'term', TYPE_STR);
    	if ($term != ''){$term_db .= "AND f.field8 like '%$term%' OR f.field10 like '%$term%' OR f.field12 like '%$term%' OR f.field13 like '%$term%' OR f.field21 like '%$term%' "; }
    The problem is:
    The full text is "leonardo dicaprio"
    If i search 'leo' or 'leonardo' or 'dicaprio' or 'leonardo dicaprio' is ok.
    If change position or cut the words like if I search 'leo dicap' or dicaprio leonardo it won't show the result.

    Can someone out there help me how to make it possible?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    search 'leo dicap' or dicaprio leonardo it cant show the result
    I had to do this. I did it by seperating the words using strtok, then searching on the word 'chunks' as I called them.
    Then you need to decide whether both chunks or only one has to be a hit

    Comment

    • amel86
      New Member
      • Apr 2010
      • 25

      #3
      Originally posted by code green
      I had to do this. I did it by seperating the words using strtok, then searching on the word 'chunks' as I called them.
      Then you need to decide whether both chunks or only one has to be a hit
      sory code green, i not clear what you mean, can you give an example.sory, i new in php

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        From the manual
        strtok() splits a string into smaller strings (tokens), with each token being delimited by any character from token.
        That is, if you have a string like "This is an example string" you could tokenize this string into its individual words
        by using the space character as the token.
        Then which each chunk issue a separate SELECT query
        Code:
        AND f.field8 like '%$chunk%
        strtok() is one function I can never write free hand so again, example from the manual
        Code:
        string = "This is\tan example\nstring";
        /* Use tab and newline as tokenizing characters as well  */
        $tok = strtok($string, " \n\t");
        
        while ($tok !== false) {
            echo "Word=$tok<br />";
            $tok = strtok(" \n\t");
        }
        If I can find my code I will post but I am sure it is too fragmented across multiple functions to be much use.
        Pretty awful if I remember

        Comment

        • amel86
          New Member
          • Apr 2010
          • 25

          #5
          i have use this function but it still not too smart search..
          if i search it just read the first word.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            You can't mix AND and OR
            Code:
            AND f.field8 like '%$term%' OR f.field10 like '%$term%' OR f.field12 like '%$term%' OR f.field13 like '%$term%' OR f.field21 like '%$term%'
            Brackets should be used
            Code:
            AND (f.field8 like '%$term%' 
            OR f.field10 like '%$term%' 
            OR f.field12 like '%$term%' 
            OR f.field13 like '%$term%' 
            OR f.field21 like '%$term%')

            Comment

            • amel86
              New Member
              • Apr 2010
              • 25

              #7
              thanks code green, but the problem still same. do you have any idea to make the search result more smarter like os commerce (osc)

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                I reckom I am smarter than OS commerce ;-)
                Could you redfine the problem and post relevant code

                Comment

                • amel86
                  New Member
                  • Apr 2010
                  • 25

                  #9
                  Code:
                  $term = $vbulletin->input->clean_gpc('r', 'term', TYPE_STR);
                  	if ($term != '')
                  	    {
                  		$token = strtok($term, " ");	
                  		$term_db .= "AND f.field8 like '%$token%'"; 
                  		}
                  the problem is :

                  if i search 'melaka'
                  the result are :
                  - Seri Costa Hotel Melaka
                  - Phiten Shop (Mahkota Parade) - Melaka
                  - Melaka Batik House (Noor Alfa)
                  - Melaka Art Store

                  but if i search 'melaka art'
                  the result still same because of it keep 'melaka' value.
                  how if i want the result just show
                  - Melaka Art Store

                  can u help me?

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    You need to use strtok() in a while loop as in the example I posted.
                    Then you can build up the query string in the while loop using OR, or query each part seperately
                    Code:
                    $term_db .= "AND f.field8 like'"; 
                    $tok = strtok($string," ");
                    $term_db .= "%$tok%".' OR '; 
                     
                    while ($tok !== false) {
                            $tok = strtok($string," "); 
                            $term_db .= "%$tok%".' OR '; 
                    }

                    Comment

                    • amel86
                      New Member
                      • Apr 2010
                      • 25

                      #11
                      hello code green,
                      i have try your code but my page appear blank. i cant use while method.can it possible to use 'for loop'? i have try 'for loop', that is no error but this method cant find any result.can you give me example if do in for loop?

                      Comment

                      • amel86
                        New Member
                        • Apr 2010
                        • 25

                        #12
                        hye...i already solve this problem by use this
                        Code:
                        $tok = strtok($term, " "); 
                        				while ($tok !== FALSE)
                        				{
                        				  $toks[] = $tok;
                        				  $tok = strtok(" ");
                        				}
                        				while (list($k,$v) = each($toks))
                        				{
                        				 $term_db .= "AND f.field8 like '%$v%'"; 
                        				}
                        tq green code for your great idea!

                        Comment

                        Working...