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

Asked

Viewed 1,213 times

1

I’m setting up a simple search engine for a paper bank at a student science event. Adapting codes of some guides I found on the internet the system is working 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 put in the search exactly "Organization of Information".

I found a topic here in Stack that seems to me to contain the solution of the problem in the answers, however I could not apply in 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, in how to fit the tips in my system or if you have a better way of doing, please give me a light.

Follow the PHP code I’m using for the search (I removed only the pagination 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 for your help.

Hugs!

  • Exactly here is the solution: http://answall.com/a/13181/7210 where you’re having trouble.

  • Jorge, I just mentioned this link in my doubt. The problem is that it didn’t work. I used it as follows: I replaced the string "$search = 'green car smashed ';" with "$search = $search".

  • This link is the same for your problem, when you implemented came to give some result?

  • I tried to exchange AND for OR, but nothing. It just keeps returning the exact terms. If any word is between them, the search returns nothing.

1 answer

2

The problem was just lack of knowledge in PHP, but after many attempts modifying the code of the link above, I managed. Follow the result so that 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.

Browser other questions tagged

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