note 62386 deleted from function.strrev by betz
| From: | betz@php.net | Date: | Mon, 27 Feb 2006 16:30:55 +0000 |
| Subject: | note 62386 deleted from function.strrev by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-104495@lists.php.net to get a copy of this message | ||
Note Submitter: portos_ze_retour at hotmail dot fr
----
I developped this function to improve a search in a database :
function SearchEngine($field, $criteria) {
$criterias = explode(" ",$criteria);
$string="";
for($i=0; $i<count($criterias) ; $i++) {
$string .= " OR ". $field ." LIKE '%". implode("%",$criterias)
."%'";
$criterias[count($criterias)-1] = array_shift($criterias); // swap the array values for 1 position
}
return substr($string,4); // return the string without the first " OR ";
}
In fact, this function takes as attributes $field (name of the field in the database) and $criteria,
which is the string given by the user to search (words are separeted by ' '), and creates
a search string.
echo SearchEngine("Field_Name", "word1 word2 word3");
should return :
Field_Name LIKE '%word1%word2%word3%' OR Field_Name LIKE '%word2%word3%word1%'
OR
Field_Name LIKE '%word3%word1%word2%'
I use it like that :
mysql_query("SELECT DISTINCT * FROM Table_Name WHERE
".SearchEngine('Field_Name','word1 word2').";");
I know that I don't do all the combinations, but it improves really the search.
P.S.: Don't forget to use the "DISTINCT" attribute in your SQL query