Jump to content


Photo

[SOLVED] php search


  • Please log in to reply
6 replies to this topic

#1 abdfahim

abdfahim
  • Members
  • PipPipPip
  • Abd Fahim
  • 535 posts

Posted 24 August 2006 - 04:53 AM

I am designing a php-mysql base search page. I already design the page so that anyone can search a single word. But I want that if anyone type multiple words, it will search for each word separately. For that I use the following code –

$search_string =$_POST['srch_nm'];
$pre_filter=trim($search_string);
$get_search=explode(" ",$pre_filter);
foreach ($get_search as $final_string){
$posts =mysql_query("SELECT * FROM table_name WHERE  column_name LIKE '%$final_string%' ");
}
$n=0;
while($row=mysql_fetch_assoc($posts)){
$n++;
echo $row['post'];
echo "<br>";
}

Now the problem is, if I type “Quick Brown Fox”, it returns search result for only “Fox”. Because for every loop of “foreach” loop, the value of “$posts” is replaced by latest word. So the reason is clear to me, but the solution is not. What can I do??


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 August 2006 - 04:59 AM

well the easy answer is to put the rest of your code inside the foreach.  move the foreach's } bracket down to the bottom, under the last }.

the better answer would be to not explode your string, and change your query to something like this:

select * from table_name where column_name in ('$get_search')
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 abdfahim

abdfahim
  • Members
  • PipPipPip
  • Abd Fahim
  • 535 posts

Posted 24 August 2006 - 05:17 AM

The problem with your better answer is that it does not work here. For test I directly query in phpmyadmin with following code
SELECT * FROM table_name WHERE column_name LIKE ('%quick Brown%')

Also I check like

SELECT * FROM table_name WHERE column_name LIKE ('quick Brown')

But it shows zero result, where individually “quick” and “brown” shows 5 and 10 results respectively (no common between them). So it should return 15. Isn’t it?


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 August 2006 - 05:29 AM

umm, well how about this:

select * from table_name where column_name like '%quick%' or column_name like '%brown%'

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 abdfahim

abdfahim
  • Members
  • PipPipPip
  • Abd Fahim
  • 535 posts

Posted 24 August 2006 - 05:48 AM

That will work 4 sure. But how can you do this without command "explode" or something. Coz the visitor in the website has only one textbox and he will definitely type "Quick Brown". Like in google, will you have multiple text box so that you can typr "quick" in one and "brown" in other??

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 August 2006 - 05:57 AM

well, with that last query, you would actually want to keep the explode command, and dynamically build your query string, seeing as how the user could put a variable number of words in his search string.  you would want it to look something like this:

$search_string =$_POST['srch_nm'];
$pre_filter=trim($search_string);
$get_search=explode(" ",$pre_filter);
$sql = "select * from table_name";
foreach ($get_search as $final_string){
  $sql .= " where column_name like '%final_string%' or";
}
$sql = substr_replace($sql,"",-2); //take off the last 'or'
$posts =mysql_query($sql);
}
$n=0;
while($row=mysql_fetch_assoc($posts)){
$n++;
echo $row['post'];
echo "<br>";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 abdfahim

abdfahim
  • Members
  • PipPipPip
  • Abd Fahim
  • 535 posts

Posted 24 August 2006 - 06:02 AM

Great BOSS. I think this will work. Thanx.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users