Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written code to retrieve database data according to multiple words entered into a search input. It's working but the data retrieved is not coming back in the same order it was entered into the search input/foreach loop. any suggestions?

thanks

What I have tried:

$word=$_POST['word']; 
$word3 = $_POST['word']; 
$word = explode(";", $word);
$noOfWords = count($word);
$word2 = $word3;

if($noOfWords == 1){
    $searchString = " word_eng LIKE '".$word3."%'";
    
}
else { 

$searchString = $whereClause = "";
foreach($word as $word){
  $searchString .= " OR word_eng LIKE '".$word."'";
  
  echo $word;
}

}

$whereClause = ($searchString != "") ? " WHERE ".preg_replace('/OR/', '', $searchString, 1) : $whereClause;

$sql = "SELECT word_eng FROM words ".$whereClause ." LIMIT 17 ";
Posted
Updated 26-Aug-17 0:19am

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:
PHP
$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:
PHP
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.
 
Share this answer
 
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900