Here's one way I did something like this for a job search site (jobs.thetaxi.org). It searches for location, category, and through a title and description field for records. I tried to comment it as best as I could. It basically creates a bunch of little strings that are applied after a LIKE statement to make a huge query. Works fine on small databases, but is probably inefficient for anything large. Let me know if you want me to clarify anything...
It starts by adding the location and category to the criteria. Then it checks to see if the words are enclosed in quotes, "Visual Basic", and if so, it'll search for both of those words next to each other. If there are no quotes, it'll break up the words and add an element to the criteria looking for each word. These are either seperated by && or ||, depending on the value of a radio button, "find all words" or "find any word". Then it assembles everything into a huge query.
To see it work, visit jobs.thetaxi.org/search.php3
$element = 0;
// create element in criteria array of location = ##
if ($location > 1) { $criteria[$element++] = " location='$location' "; }
// create element in criteria array of category = ##
if ($category > 1) { $criteria[$element++] = " category='$category' "; }
//check to see if keywords are enclosed in quotes
if (substr($keywords,0,1) == "\"" && substr($keywords,-1) == "\"")
{
//strip off quotes
$keywords = substr($keywords,1,-1);
$kw_criteria[0] = " title LIKE '%$keywords%' ";
$kw_criteria[1] = " description LIKE '%$keywords%' ";
//end up with statment looking for title OR description
//containing 'keywords', enclosed by ()
$criteria[$element++] = " ( " . implode(" || ",$kw_criteria) . " ) ";
}
else
{
//check to see if anything was put in keywords field
if (strlen($keywords) > 0)
{
//seperate all keywords in array.
$words_array = explode(" ",$keywords);
//count how many words there are
$num_words = count($words_array);
$kw_element = 0;
for($count=0;$count<$num_words;$count++)
{
//for each word, create criteria looking for title OR description
//matching the word, enclosed within ()
$kw_criteria[$kw_element] = " ( title LIKE '%" . $words_array[$count] . "%' ";
$kw_criteria[$kw_element] .= " || ";
$kw_criteria[$kw_element++] .= " description LIKE '%" . $words_array[$count] . "%' ) ";
}
//join each title || description pair for each word into one string
//seperated by $match, which is either && or ||
//&& = match all words
//|| = match any word
$criteria[$element++] = " ( " . implode(" $match ",$kw_criteria) . " ) ";
}
}
//finally, join all of the criteria into the final criteria statement,
//seperated by && and enclosed by ()
//the && at the beginning is needed because the SQL statement always does
//a search for expired dates && final_criteria when searching for jobs
if (count($criteria) > 0) { $final_criteria = " && ( " . implode(" && ",$criteria) . " ) "; }
if (isset($search_jobs) || $list=="jobs")
{
//construct final query. end_date > NOW()-1 gives us dates greater
//than yesterday,so jobs expiring today aren't left out.
$query = "SELECT id,date_format(end_date,'b d, Y'),name,email,title,company FROM jobs WHERE (end_date>NOW()-1) $final_criteria ORDER BY end_date ASC";
}