943,170 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 38
  • PHP RSS
Sep 3rd, 2010
0

Higher Search

Expand Post »
HI All,

my requirement is as below:
lets product name is "aaa bbb ccc".
Now in search coding i have use LIKE statement.
PHP Syntax (Toggle Plain Text)
  1. "... where product_name LIKE '%".$keyword."%'"

Now when user insert 'aaa' or 'bbb' or 'ccc' or 'aaa bbb' or 'bbb ccc' search works.
But if user insert 'aaa ccc' then it is not working.
I want such search result where 'aaa ccc' will also comes in result.

Help me !!
Similar Threads
Reputation Points: 154
Solved Threads: 162
Master Poster
vibhadevit is offline Offline
791 posts
since Apr 2010
Sep 3rd, 2010
0
Re: Higher Search
Just a quick thought: you can explode your search term in to words and check with each of them.
Reputation Points: 14
Solved Threads: 9
Light Poster
Daiva is offline Offline
37 posts
since May 2009
Sep 3rd, 2010
0
Re: Higher Search
I think it wont work,

e.g product name "blackberry bold 2200",
and i would search with "blackberry 2200"
then if i explode then all "blackberry" would comes.. i just want blackberry 2200.

This is also what google do.
But i don't know how they checks?
Reputation Points: 154
Solved Threads: 162
Master Poster
vibhadevit is offline Offline
791 posts
since Apr 2010
Sep 3rd, 2010
1
Re: Higher Search
I am not a professional person... But shouldn't it be working?
PHP Syntax (Toggle Plain Text)
  1. "... where (product_name LIKE '%".$keyword[1]."%') AND (product_name LIKE '%".$keyword[2]."%')..."

Hope somebody will help you. I am interested in this problem too :]
Last edited by Daiva; Sep 3rd, 2010 at 4:45 am.
Reputation Points: 14
Solved Threads: 9
Light Poster
Daiva is offline Offline
37 posts
since May 2009
Sep 3rd, 2010
0
Re: Higher Search
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $_POST['keywords']="aaa ccc";
  4.  
  5. for($i=0;$i<10;$i++)
  6. $_POST['keywords']=str_replace(" "," ",$_POST['keywords']);
  7.  
  8. $arrkey=explode(" ",trim($_POST['keywords']));
  9.  
  10.  
  11. $query="select * from product where somecol='somevalue' ";
  12.  
  13. $keycount=count($arrkey);
  14. if($keycount>0 and trim($_POST['keywords'])!="")
  15. {
  16. $query.=" and (";
  17. $operator="";
  18. for($i=0;$i<$keycount;$i++)
  19. {
  20. $query.=$operator."(product_name like '%{$arrkey[$i]}%')";
  21. $operator=" or ";
  22. }
  23. $query.=")";
  24. }
  25. echo $query;
  26.  
  27. ?>
Reputation Points: 245
Solved Threads: 259
Nearly a Posting Virtuoso
urtrivedi is offline Offline
1,248 posts
since Dec 2008
Sep 3rd, 2010
0
Re: Higher Search
Click to Expand / Collapse  Quote originally posted by urtrivedi ...
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $_POST['keywords']="aaa ccc";
  4.  
  5. for($i=0;$i<10;$i++)
  6. $_POST['keywords']=str_replace(" "," ",$_POST['keywords']);
  7.  
  8. $arrkey=explode(" ",trim($_POST['keywords']));
  9.  
  10.  
  11. $query="select * from product where somecol='somevalue' ";
  12.  
  13. $keycount=count($arrkey);
  14. if($keycount>0 and trim($_POST['keywords'])!="")
  15. {
  16. $query.=" and (";
  17. $operator="";
  18. for($i=0;$i<$keycount;$i++)
  19. {
  20. $query.=$operator."(product_name like '%{$arrkey[$i]}%')";
  21. $operator=" or ";
  22. }
  23. $query.=")";
  24. }
  25. echo $query;
  26.  
  27. ?>
This code will generate below query:
PHP Syntax (Toggle Plain Text)
  1. $query="select * from product where somecol='somevalue' and ( (product_name like '%aaa%') or (product_name like '%ccc%') ) ";

But again, this will popup all record which have 'aaa' but not 'ccc'.
Reputation Points: 154
Solved Threads: 162
Master Poster
vibhadevit is offline Offline
791 posts
since Apr 2010
Sep 3rd, 2010
0
Re: Higher Search
Click to Expand / Collapse  Quote originally posted by Daiva ...
I am not a professional person... But shouldn't it be working?
PHP Syntax (Toggle Plain Text)
  1. "... where (product_name LIKE '%".$keyword[1]."%') AND (product_name LIKE '%".$keyword[2]."%')..."

Hope somebody will help you. I am interested in this problem too :]
This is simple logic and it should work.. let me check.
Reputation Points: 154
Solved Threads: 162
Master Poster
vibhadevit is offline Offline
791 posts
since Apr 2010
Sep 3rd, 2010
0
Re: Higher Search
CHANGE MY LINE NO 21 OF PREVIOUS POST LIKE GIVEN BeLOW, remove or then write AND

$operator=" and ";
Reputation Points: 245
Solved Threads: 259
Nearly a Posting Virtuoso
urtrivedi is offline Offline
1,248 posts
since Dec 2008
Sep 3rd, 2010
0
Re: Higher Search
Thanks urtrivedi and Daiva.
The logic of both is same and it is so simple.
I dont know why it was not sparked for me even its so simple, anyways it happens sometimes
Reputation Points: 154
Solved Threads: 162
Master Poster
vibhadevit is offline Offline
791 posts
since Apr 2010

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: validating user input... question?
Next Thread in PHP Forum Timeline: which Framework should i use ??





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


Follow us on Twitter


© 2011 DaniWeb® LLC