How can you setup you web seach to do a multi word like search?
I have a search on my site now that I put in.. it works great if you put in one word and spell it correct... but if you do not spell it right or have more that one word...
your results are 0...
if they have multi words and they are spelt correct and the way I have them in the table they will show...
SELECT * FROM campgrounds WHERE (`name` LIKE'%$find%') OR (`city` LIKE'%$find%') OR (`notes` LIKE'%$find%') OR (`moreinfo` LIKE'%$find%') OR (`amenities` LIKE'%$find%') ORDER BY `name`
Using this search..
I would search for New Old Town and it would give me 0 results.
if I just search town I get 15 results
if I search just old I get 20 results
and if I search for new I get 7 results.
Not sure what I have done wrong.
Thanks... all help appreciated.
Slayer.
Last edited by SlayerACC; 02-20-2013 at 09:19 PM..
You *MUST* split up the $find into SEPARATE WORDS and use LIKE on *EACH* word!
So if $find is "New Old Town" your query would need to be:
Code:
SELECT * FROM campgrounds
WHERE (name LIKE '%new%' AND name LIKE '%old%' AND name LIKE '%town%')
OR (city LIKE '%new%' AND city LIKE '%old%' AND city LIKE '%town%')
OR (notes LIKE '%new%' AND notes LIKE '%old%' AND notes LIKE '%town%')
OR (moreinfo LIKE '%new%' AND moreinfo LIKE '%old%' AND moreinfo LIKE '%town%')
OR (amenities LIKE '%new%' AND amenities LIKE '%old%' amenities city LIKE '%town%')
ORDER BY name
The above assumes you want to find ALL THREE WORDS (in any order or place) in the SAME field (e.g., maybe moreinfo contains "This town has more old buildings than old."
If finding *ANY* ONE of the words in any of the fields is good enough, then replace all the AND with OR instead.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Pardon me, but that's impossible if you actually used the code I showed.
In my code $sql is nothing but a string. That looks like you echoed the $result.
And if the OR version worked, the AND version should work, too. But only if you actually had some data that matched the condition! In the $pattern that you showed:
Code:
## LIKE '%new%' AND ## LIKE '%old%'
that would mean that one of the tested fields has to contain *BOTH* "new" and "old".
As I said, it depends on what you want.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
You know, you COULD offer you users the choice of "Find matches on any words" vs. "Find matches on all words".
Do something like this:
Code:
<label>
<input type="radio" name="delimiter" checked value=" OR " />
Find matches on any one or more words
</label>
<label>
<input type="radio" name="delimiter" value=" AND " />
Find matches only on all words
</label>
And then in your PHP code, you do:
Code:
$delimiter = $_POST["delimiter"];
$search = $_POST["search"];
$words = explode( " ", $search ); // get array of words
$pattern = "";
for ( $w = 0; $w < count($words); ++$w )
{
$word = $wods[$w];
if ( $word != "" )
{
if ( $pattern != "" ) $pattern .= $delimiter;
$pattern .= " ## LIKE '%" . $word . "%' ";
}
}
... rest same ...
Now you have the best of both worlds: Both kinds of searches for the cost of a pair of radio buttons!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.