Re: Help with search engine and splitting values needed :-(
| From: | Trygve | Date: | Tue, 16 Oct 2001 18:42:26 +0000 |
| Subject: | Re: Help with search engine and splitting values needed :-( | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-13386@lists.php.net to get a copy of this message | ||
Try this function to search multiple fields for multiple words:
// $fields is an array of fieldnames to search on
// $words is an array of search words
function buildSearch($fields, $words) {
for ($i = 0; $i < count($words); $i++) {
for ($j = 0; $j < count($fields); $j++) {
$arr[] = $fields[$j] . " LIKE '%" . $words[$i] . "%'";
}
}
return join(" OR ", $arr);
}
Example:
$query = "afr trib"; // your search words from eg. a form
$words = split(" ", $query); // split the words into an array
$fields = array("title, text"); // Field names you want to search in
$sql = "select * from table where " . buildSearch($fields, $words);
This will return all rows with any of the specified words in
any of the specified fields.
Chris Payne wrote:
Hi there everyone, I currently have a partially written search engine for a client and it works fine using the WHERE / LIKE settings (I'm using MySQL with the latest PHP for Windows to program). My question is this: Currently if you enter a single word it searches fine - if you enter afr it will bring all entries for africa or anything similar to afr which is what I need. However, how do I make it so that if they enter several words on the line that it searches for each word? I know it's probably straight forward to most people, but i'm stumped. I guess it needs to split the string from the form and then run the LIKE command on each string, but I don't know how to split the string and then get the DB to read each one? Any help would be sincerely appreciated - in newbie form though please :-) Thank you. Chris Payne chris@planetoxygene.com http://www.planetoxygene.com