I have a search problem :(
I have 4 text columns, mix of Varchar and Text and I need to find rows in a table where all words searched for are present across the 4 columns.
The 4 columns are :
name
type
keywords
description
So if someone searches for "london wildlife museum" it would only return rows where all words were found across the 4 columns
Current code to manage the multiple words is
$words = $_GET['freetext'];
if(empty($words)){
//redirect somewhere else!
}
$parts = explode(" ",trim($words));
$clauses1=array();
foreach ($parts as $part){
//function_description in my case , replace it with whatever u want in ur table
$clauses1[]="vname LIKE '%" . mysql_real_escape_string($part) . "%'";
}
$clause1=implode(' OR ' ,$clauses1);
$parts = explode(" ",trim($words));
$clauses2=array();
foreach ($parts as $part){
//function_description in my case , replace it with whatever u want in ur table
$clauses2[]="vtype LIKE '%" . mysql_real_escape_string($part) . "%'";
}
$clause2=implode(' OR ' ,$clauses2);
$parts = explode(" ",trim($words));
$clauses3=array();
foreach ($parts as $part){
//function_description in my case , replace it with whatever u want in ur table
$clauses3[]="vdesc LIKE '%" . mysql_real_escape_string($part) . "%'";
}
$clause3=implode(' OR ' ,$clauses3);
$parts = explode(" ",trim($words));
$clauses4=array();
foreach ($parts as $part){
//function_description in my case , replace it with whatever u want in ur table
$clauses4[]="vkeywords LIKE '%" . mysql_real_escape_string($part) . "%'";
}
$clause4=implode(' OR ' ,$clauses4);
//select your condition and add "AND ($clauses)" .
$sql="SELECT vid, vname, vsuburb, vtype, vlogo, suburb.sname
FROM venue, suburb
WHERE
venue.vsuburb = suburb.sid
AND (($clause1) OR ($clause2) OR ($clause3) OR ($clause4))";
Obviously this creates a long list of OR's but I can't see how to only choose the rows where all words appear.
Any help would be appreciated
Cheers