Google like search engine with php
| From: | Graham Anderson | Date: | Sun, 05 Dec 2004 18:14:11 +0000 |
| Subject: | Google like search engine with php | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-203530@lists.php.net to get a copy of this message | ||
What is the proper way to build a dynamic subquery ?
I want to allow the user to use AND, OR, and NOT in his/her query
like $query = "Hola AND Adios, OR Goodbye, NOT hello"
or..
WHERE (greeting LIKE %Hola% AND greeting LIKE %Adios%) OR (greeting LIKE
%Goodbye%) AND (greeting NOT LIKE %hello%)
I am a bit new at this so any help would be appretiated :)
My Guess:
AND,OR,NOT,'+', and '-' would be extracted from query and put into an array.
So all the operators become $wordOperator[ ]
where:
'hola' : $wordOperator[0] = "AND"
'Adios': $wordOperator[1] = "AND"
'greeting' : $wordOperator[2] = "OR"
'hello' : $wordOperator[3] = "NOT"
// My script in its current state
$query = "Hola Adios" //taken from a GET request
//parse by a space
$searchwords = explode(" ",$query);
$x = 1;
$operator = "AND";
$not_search_word = array('if', 'for', 'in', 'on','and','with');
//taken from a GET request
$searchwords = explode(" ",$query);
foreach ($searchwords as $word) {
if (!in_array($word,$not_search_word)){ //relevant search term only
//if we are on the first word...format subquery like this
if ($x == 1){
$sql .= ' AND(media.'.$language.' Like "%'.$word.'%"';
$sql .= ' OR artist.'.$language.' Like "%'.$word.'%"';
$sql .= ' OR artist.name Like "%'.$word.'%")';
$x++ ;
//formatting for everything past 1st word
}else{
$sql .= ' '.$operator.'(media.'.$language.' Like "%'.$word.'%"';
$sql .= ' OR artist.'.$language.' Like "%'.$word.'%"';
$sql .= ' OR artist.name Like "%'.$word.'%")';
}
}} $sql .= ' ORDER BY artist.name ASC,'; $sql .= ' media.'.$language.' ASC LIMIT 0,60';