Search with multiple search terms used

Thread Solved

Join Date: Sep 2009
Posts: 11
Reputation: empoor is an unknown quantity at this point 
Solved Threads: 0
empoor empoor is offline Offline
Newbie Poster

Search with multiple search terms used

 
0
  #1
Sep 17th, 2009
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.

  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
Cordialement,

Remy L. Overkempe
Martian Prince
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Search with multiple search terms used

 
0
  #2
Sep 17th, 2009
Explode on spaces and use ORs for the extra terms. http://php.net/explode
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: empoor is an unknown quantity at this point 
Solved Threads: 0
empoor empoor is offline Offline
Newbie Poster

Re: Search with multiple search terms used

 
0
  #3
Sep 18th, 2009
Originally Posted by ShawnCplus View Post
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.
Cordialement,

Remy L. Overkempe
Martian Prince
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: empoor is an unknown quantity at this point 
Solved Threads: 0
empoor empoor is offline Offline
Newbie Poster

Re: Search with multiple search terms used

 
0
  #4
Sep 19th, 2009
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.

  1. do
  2. {
  3. $adid_array[] = $row['page_id'];
  4. }
  5.  
  6. while($row = mysql_fetch_array($numresults));
  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
  1. foreach ($search_array as $search_keyword)
, and wrote a little code to separate each keyword with AND.

  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
Cordialement,

Remy L. Overkempe
Martian Prince
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 2
dasatti dasatti is offline Offline
Light Poster

Re: Search with multiple search terms used

 
0
  #5
Sep 19th, 2009
Try this
  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.
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Reply

Tags
keywords, multiple, mysql, php, search

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC