How to search for multiple order independent terms?

Asked

Viewed 230 times

2

my scenario and the following, I have an input where the user puts the term and clicks on search

EX: car horizon [ENTER]

however you are listing the result of the separate words

EX: 3 results found

id1 - house in beautiful horizon

id2 - accept car in exchange

id3- car located in beautiful horizon

I would like you to display only the result that contains the 2 words

EX: 1 result found

id1- car located in beautiful horizon

?>
$search_term = filter_var($_GET["s"], FILTER_SANITIZE_STRING);
$palavras = explode(" ", $search_term);
for ($i = 0; $i < count($palavras); $i++) 
$q = "SELECT * FROM classificados WHERE texto LIKE '%".$palavras[$i]."%' AND aprovado='s' ORDER BY ID desc";
$r = mysql_query($q);
if(mysql_num_rows($r)==0)//no result found
{
echo "<div id='search-status'>Nenhum resultado encontrado!</div>";
}
else //result found
{
echo "<ul>";
while($row = mysql_fetch_assoc($r))
    {
    $title = $row['texto'];
?>

I hope I have well illustrated my doubt

  • 1

    If I understood the question correctly, you would have to put each word in a type like this: "SELECT * FROM table WHERE text LIKE '%". $palavras1." %' AND LIKE '%". $palavras2." %' AND aprovado=’s' ORDER BY ID desc"; that’s about it ?

  • 2

    Isn’t this what you want? If it is, the code is practically ready in the answer. http://answall.com/a/13181/70

  • diego segue $palavras = explode(" ", $search_term); for ($i = 0; $i < Count($words); $i++) $q = "SELECT * FROM classifieds where text = '%". $palavras[$i]." %' AND aprovado=’s' ORDER BY ID desc";

  • 1

    In parts it is true @Bacco, but he may be wanting the treatment to split the two words of the input.

  • vi yes Diego however the search is only in a field ( text) in this result the very like ja made, and what I would like is to only list the results containing the keywords in the same text

  • See if you can help http://forum.imasters.com.br/topic/509139-encontrara-palavra-como-elafoi-typed/

  • Good Motta I thought this problem was simpler to solve, but I took a look at MATCH AGAINST and it can be that I will solve even though I will leave to study tomorrow this part because I am working now

Show 2 more comments

1 answer

4


Follow modification in your loop to mount the query:

 $search_term = filter_var( $_GET['s'], FILTER_SANITIZE_STRING );
 $palavras = explode( ' ', $search_term );
 $q = 'SELECT * FROM classificados WHERE';
 for ( $i = 0; $i < count($palavras); $i++ ) { 
    $q .= " texto LIKE '%" . $palavras[$i] . "%' AND " ;
 }
 $q .= " aprovado='s' ORDER BY ID desc";
 $r = mysql_query( $q );

 if( mysql_num_rows( $r )==0 ...


If you prefer the exact search you don’t take carros instead of carro, just use it like this:

 $search_term = filter_var( $_GET['s'], FILTER_SANITIZE_STRING );
 $palavras = explode( ' ', $search_term );
 $q = 'SELECT * FROM classificados WHERE';
 for ( $i = 0; $i < count( $palavras ); $i++ ) { 
    $q .= " CONCAT( \" \", texto, \" \" ) LIKE '% " . $palavras[$i] . " %' AND "; 
 }
 $q .= " aprovado='s' ORDER BY ID desc";
 $r = mysql_query($q);

Be careful in this case to keep the spaces as they are in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.