You should use better variable names to make it understable what happens and should not use the same name for different types in your
foreach
loop:
$word_string = $_POST['word'];
$word_array = explode(";", $word_string);
$noOfWords = count($word_array);
if ($noOfWords == 1) {
$searchString = " word_eng LIKE '".$word_string."%'";
}
else {
foreach ($word_array as $word) {
$searchString .= " OR word_eng LIKE '".$word."'";
echo $word;
}
}
I don't know if that solves your problem, but that is much more understable from my point of view.
Instead of removing the preceding "OR" with multiple words, I would do it this way:
if ($noOfWords == 1) {
$searchString = " WHERE word_eng LIKE '".$word_string."%'";
}
else {
$searchString = " WHERE word_eng LIKE '".$word_array[0]."'";
for ($i = 1; $i < $noOfWords; $i++) {
$searchString .= " OR word_eng LIKE '".$word_array[$i]."'";
}
}
Quote:
the data retrieved is not coming back in the same order it was entered into the search input
That is expected because the data are provided by the database which usually returns the matches in the order they appear. If you want to have them ordered by the search words, you must query each single word.