943,721 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2732
  • PHP RSS
Sep 17th, 2009
0

Search with multiple search terms used

Expand Post »
Hi. I tried looking for this problem on Google, and found a lot, but nothing that actually seems to work. I basically have a search script that works fine when using one keyword, but when one uses more than that, it returns every single row per keyword. So a search for "hello kitty" doesn't look for "hello" and "kitty", but for "hello" and then another time for "kitty".

I know there's something wrong with my code, but, what? And how can I solve this issue.

PHP Syntax (Toggle Plain Text)
  1. // get the search variable from URL
  2. $var = @$_GET['q'] ;
  3. // trim whitespace from the stored variable
  4. $trimmed = trim($var);
  5. // separate key-phrases into keywords
  6. $trimmed_array = explode(" ",$trimmed);
  7.  
  8. // build SQL Query for each keyword entered
  9. foreach ($trimmed_array as $trimm)
  10. {
  11.  
  12. $query = "SELECT * FROM (articles LEFT JOIN authors ON articles.author_id = authors.author_id) WHERE text LIKE '%$trimm%' ORDER BY issue DESC" ;
  13.  
  14. // and then the queries and everything; with which nothing's wrong
  15.  
  16. }

Can someone help me?
Last edited by empoor; Sep 17th, 2009 at 5:16 pm. Reason: clarification
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
empoor is offline Offline
11 posts
since Sep 2009
Sep 17th, 2009
0

Re: Search with multiple search terms used

Explode on spaces and use ORs for the extra terms. http://php.net/explode
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 18th, 2009
0

Re: Search with multiple search terms used

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
Explode on spaces and use ORs for the extra terms. http://php.net/explode
I'm already using explode to separate terms. And shouldn't it be "AND", 'cause I want the results to match all the results. It's basically already doing an "OR" search, which results in duplicate search results showing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
empoor is offline Offline
11 posts
since Sep 2009
Sep 19th, 2009
0

Re: Search with multiple search terms used

I have solved the issue myself by trying to fix things with experimentation.

My first problem was that the code I used to remove duplicate entries from the search array was basically looping twice. So, I edited the source to keep it from looping twice.

PHP Syntax (Toggle Plain Text)
  1. do
  2. {
  3. $adid_array[] = $row['page_id'];
  4. }
  5.  
  6. while($row = mysql_fetch_array($numresults));
PHP Syntax (Toggle Plain Text)
  1. $tmparr = array_unique($adid_array);
  2. $i = 0;
  3. foreach ($tmparr as $newarr_v)
  4. {
  5. $newarr[$i] = $newarr_v;
  6. $i++;
  7. }

And then I use newarr to execute the search query (with foreach).

Secondly, because this is my first experience with building a search page, I used stock code and didn't notice that it was erroneously building a query for each keyword entered, instead of a query that incorporates all keywords (if multiple). So I removed
PHP Syntax (Toggle Plain Text)
  1. foreach ($search_array as $search_keyword)
, and wrote a little code to separate each keyword with AND.

PHP Syntax (Toggle Plain Text)
  1. $var = addslashes(htmlspecialchars($_GET['q'])) ;
  2. // trim whitespace from the stored variable
  3. $trimmed = trim($var);
  4. // separate key-phrases into keywords
  5. $trimmed_array = explode(" ",$trimmed);
  6. // count keywords
  7. $trimm_total = count($trimmed_array);
  8. $i = 0;
  9. $searchstring = '';
  10.  
  11. // looping to get the search string
  12. foreach ($trimmed_array as $trimm)
  13. {
  14. if ($i != 0 and $i != $wordcount)
  15. {
  16. $searchstring .= " AND ";
  17. }
  18.  
  19. $searchstring .= "text LIKE '%$trimm%' OR page_title LIKE '%$trimm%' OR author LIKE '%$trimm%'";
  20.  
  21. // incrementing the value
  22. $i = $i + 1;
  23. }

And now it finally works.
Last edited by empoor; Sep 19th, 2009 at 5:29 am. Reason: clarification
Reputation Points: 10
Solved Threads: 0
Newbie Poster
empoor is offline Offline
11 posts
since Sep 2009
Sep 19th, 2009
0

Re: Search with multiple search terms used

Try this
php Syntax (Toggle Plain Text)
  1. // get the search variable from URL
  2. $var = @$_GET['q'] ;
  3. // trim whitespace from the stored variable
  4. $trimmed = trim($var);
  5. // separate key-phrases into keywords
  6. $trimmed_array = explode(" ",$trimmed);
  7.  
  8. // build SQL Query for each keyword entered
  9. $query = "SELECT * FROM (articles LEFT JOIN authors ON articles.author_id = authors.author_id) WHERE";
  10.  
  11. $first = true;
  12. foreach ($trimmed_array as $trimm)
  13. {
  14. if(!$first)
  15. {
  16. $query . = " AND ";
  17. }
  18. else
  19. {
  20. $first = false;
  21. }
  22.  
  23.  
  24. $query = $query . " text LIKE '%$trimm%' " ;
  25.  
  26. // and then the queries and everything; with which nothing's wrong
  27.  
  28. }
  29. $query = $query. " ORDER BY issue DESC";
Last edited by dasatti; Sep 19th, 2009 at 7:38 am.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
dasatti is offline Offline
57 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: create subdomain in server dynamically using php
Next Thread in PHP Forum Timeline: Help debug Random property script.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC