note 62386 added to function.strrev
| From: | portos_ze_retour at hotmail dot fr | Date: | Mon, 27 Feb 2006 15:52:02 +0000 |
| Subject: | note 62386 added to function.strrev | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104493@lists.php.net to get a copy of this message | ||
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
----
Server IP: 85.116.0.130
Probable Submitter: 81.31.126.130
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.strrev.php
Edit -- http://master.php.net/note/edit/62386
Del: integrated -- http://master.php.net/note/delete/62386/integrated
Del: useless -- http://master.php.net/note/delete/62386/useless
Del: bad code -- http://master.php.net/note/delete/62386/bad+code
Del: spam -- http://master.php.net/note/delete/62386/spam
Del: non-english -- http://master.php.net/note/delete/62386/non-english
Del: in docs -- http://master.php.net/note/delete/62386/in+docs
Del: other reasons-- http://master.php.net/note/delete/62386
Reject -- http://master.php.net/note/reject/62386
Search -- http://master.php.net/manage/user-notes.php