I'm trying to run a search of a photos table based on tags.
I split the search term into individual keywords, and search the table for each individual keyword, pushing any matches to an array.
$keywords = explode(' ', $keywords);
$photos = array();
// loop through each keyword
foreach($keywords as $keyword){
$row = $mysqli->query(" SELECT * FROM photos WHERE tags LIKE '%$keyword%' ");
// loop through each row result
while( $query_result = $row->fetch_assoc() ){
// push result to photos array
array_push($photos, $query_result);
}
}
however, this can return the same table row multiple times, e.g if I search for 'mod modded', any photo with a tag of 'modded' would be pushed top the array twice as it matches both keywords.
How do I make sure to only select a row once?
EDIT
Thanks for the replies but neither worked, i've achieved what Im after with:
$keywords = explode(' ', $keywords);
$photos = array();
// loop through each keyword
foreach($keywords as $keyword){
$row = $mysqli->query(" SELECT * FROM photos WHERE tags LIKE '%$keyword%' ");
// loop through each row result
while( $query_result = $row->fetch_assoc() ){
$dontPush = false;
// check photos array to see if the query_result already exists
foreach($photos as $photo){
if($photo == $query_result){
$dontPush = true;
}
}
if($dontPush === false){
// push result to photos array if it dodesnt already exist
array_push($photos, $query_result);
}
}
}
but this creates so many loops that if i have a big database it will surely take a long time to return the results? Is there a better way
='$keyword'