I am trying to make a search box for an ecommerce website.
The search works as follows
When a user searches for a product, the search value is being sent to a file called searchResults.php using post method via ajax
$searchVal=$_POST['searchVal'];
And then its being searched in the database from a table named product by the following query
$searchResult = mysql_query("SELECT * FROM products WHERE name LIKE '$searchVal'")
and the results are sent back as ajax response by the following if condition
if($searchResult){
echo "result";
}
else{
echo "No products found";
}
Above all everything works fine as expected.
lets assume an user is searching for cellphones and he/she types cell phone . But we have products only for category cellphones and not for cell phone. So it results No products found even though the records for cellphones are present.
I want to make it search regardless the white space, singular or plural . How can i do that ?
The right way to implement a search engine is to maintain a separate table of words and links to the record they appear in. Then….
An ugly and innefficient hack would be:
Both methods still don’t not cater for plurals (just add an additional comparison for words ending in S, removing the S). You should also clean up the string to remove multiple spaces and punctuation (/^[a-z0-9 ]/i).
Or just use one of the many, well written off-the-shelf search engine solutions (e.g. the mnogo engine or Google’s site search service).