Jump to content

[SOLVED] php search


abdfahim

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.