I am trying to make search and need to remove word from query, that do not have match.
Lets say there is 3 words: one, two and three. If one is not found, then I need to rerun query with other two words (I get result Where empty_space
AND two
AND three
, so query finds nothing). And so on. But I don't know how to identify unmatched word and run query again with other two words.
EDIT: If I use this code, to count elements in array with $i++
, query - one two three
:
if(!empty($d)) {
if($i < 2) {
$like_str .= " (title LIKE '%".$keyword."%')";
} else {
$like_str .= " AND (title LIKE '%".$keyword."%')";
}
}
Result: SELECT * FROM shop_products_ WHERE AND (title LIKE '%two%') AND (title LIKE '%three%')
And with words that exist, query - one two
if(!empty($d)) {
if($key < 1) {
$like_str .= " (title LIKE '%".$keyword."%')";
} else {
$like_str .= " AND (title LIKE '%".$keyword."%')";
}
}
Result: SELECT * FROM shop_products_ WHERE (title LIKE '%one%') AND (title LIKE '%two%')