Search for terms "out of order" in text in PHP and MySQL

0

I am putting together a simple search system for a bank of articles from a student scientific event. By adapting codes from some guides that I found on the internet the system is running all right. The only problem is that it only returns results with exact words.

For example: If I want to return an article that talks about Information Organization and search for "Information Organization", the system returns nothing. The user needs to by exactly searching "Information Organization".

I found a topic here in the Stack that seems to contain the solution to the problem in the answers, however, I could not apply it to my system. Follow the link: Search with LIKE or MATCH..AGAINST in two columns

I ask you to analyze my code and help me, if possible, how to fit the tips in my system or if you have a better way, please give me a light.

Here is the PHP code I'm using for the search (I've removed only the paging codes from the results):

<?php
// A busca
$busca = $_GET['consulta'];
$busca = mysql_real_escape_string($busca);
$sql = "SELECT * FROM 'artigos' WHERE ('ativa' = 1) AND (('titulo' LIKE '%".$busca."%') OR ('resumo' LIKE '%".$busca."%') OR ('%".$busca."%')) ORDER BY 'grupo' DESC LIMIT ".$inicio.", ".$_BS['PorPagina'];

// Executa a consulta
$query = mysql_query($sql);

// Mostra resultados
echo "<ul>";
while ($resultado = mysql_fetch_assoc($query)) {
$titulo = $resultado['titulo'];
$resumo = $resultado['resumo'];
$link = 'http://meusite.com.br/artigos/' . $resultado['link'];
echo "<li>";
echo '<a href="'.$link.'" title="'.$titulo.'">'.$titulo.'</a><br />';
echo "<p>";
echo '<p>'.$resumo.'</p>';
echo "</li>";
}
echo "</ul>";

?>

Thanks in advance for your help.

Hugs!

    
asked by anonymous 13.04.2015 / 15:45

1 answer

1

The problem was just a lack of knowledge in PHP, but after many attempts by modifying the above link code, I succeeded. Follow the result so others with the same doubt can do.

<?php
// A busca
// Salva o que foi buscado em uma variável
$pesquisa = str_replace( array( ',', '.', '%', '-', '/', '\' ),' ', $_GET['consulta'] );
// Usa a função mysql_real_escape_string() para evitar erros no MySQL
$pesquisa = mysql_real_escape_string($pesquisa);
$palavras = explode( ' ', $pesquisa ); // dividindo as palavras pelo espaço
$palavras = array_filter($palavras); // eliminando ítens vazios

// Monta a consulta 
$sql = 'SELECT * FROM artigos ';
$cola = 'WHERE ';

//Aqui você pode juntar vários campos no concat.
$campo = 'CONCAT( titulo, " ", resumo)';

foreach ($palavras as $palavra) {
$palavra = trim($palavra); //Removendo espaços em branco
$palavra = mysql_real_escape_string($palavra); //Precisa da conexão com o banco!
$sql .= $cola.campo.' LIKE "%'.$palavra.'%" ';
$cola = 'AND ';
}
// Executa a consulta
$query = mysql_query($sql);

// Mostra resultados
echo "<ul>";
while ($resultado = mysql_fetch_assoc($query)) {
$titulo = $resultado['titulo'];
$resumo = $resultado['resumo'];
$link = 'http://meusite.com.br/artigos/' . $resultado['link'];
echo "<li>";
echo '<a href="'.$link.'" title="'.$titulo.'">'.$titulo.'</a><br />';
echo "<p>";
echo '<p>'.$resumo.'</p>';
echo "</li>";
}
echo "</ul>";

?>

Thanks for the help!

Hugs.

    
14.04.2015 / 15:54