abdfahim Posted August 24, 2006 Share Posted August 24, 2006 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 –[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>";}[/code]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?? Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/ Share on other sites More sharing options...
.josh Posted August 24, 2006 Share Posted August 24, 2006 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') Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/#findComment-79687 Share on other sites More sharing options...
abdfahim Posted August 24, 2006 Author Share Posted August 24, 2006 The problem with your better answer is that it does not work here. For test I directly query in phpmyadmin with following code[code]SELECT * FROM table_name WHERE column_name LIKE ('%quick Brown%')[/code]Also I check like[code]SELECT * FROM table_name WHERE column_name LIKE ('quick Brown')[/code]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? Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/#findComment-79693 Share on other sites More sharing options...
.josh Posted August 24, 2006 Share Posted August 24, 2006 umm, well how about this:select * from table_name where column_name like '%quick%' or column_name like '%brown%' Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/#findComment-79701 Share on other sites More sharing options...
abdfahim Posted August 24, 2006 Author Share Posted August 24, 2006 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?? Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/#findComment-79715 Share on other sites More sharing options...
.josh Posted August 24, 2006 Share Posted August 24, 2006 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:[code]$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>";}[/code] Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/#findComment-79718 Share on other sites More sharing options...
abdfahim Posted August 24, 2006 Author Share Posted August 24, 2006 Great BOSS. I think this will work. Thanx. Link to comment https://forums.phpfreaks.com/topic/18504-solved-php-search/#findComment-79720 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.