change word properties in php search

1

joao 'the system returns me a table with the referring results only that instead of showing me the normal result the system would change the background of the word that was searched, well the problem is the following I do not know if I can, or should I do this only with php, or should I use javascript, remembering that I am not asking for any code ready but rather the "best way"

follows the code for how the query results are displayed

while ($linha = $query->fetch(PDO::FETCH_ASSOC)) {

                                        echo '<tr class="odd gradeX">';
                                        echo '<td>' . $linha['NOME'] . '</td>';
                                        echo '<td>' . $linha['EMAIL'] . '</td>';
                                        echo '<td>' . $linha['LOGIN'] . '</td>';
                                        echo '<td>' . $linha['DT_NASCIMENTO'] . '</td>';
                                        echo '<td>' . $linha['DT_ULTIMOACESSO'] . '</td>';
                                        echo '<td>' . $linha['IP'] . '</td>';
                                        echo '</td>';

                                    }

The code above generates the results of this table

    
asked by anonymous 24.12.2014 / 14:55

1 answer

6
Assuming you use a query like SELECT * FROM tabela WHERE NOME LIKE ? OR EMAIL LIKE ? and a variable within the global call $_GET , the best way would be to create a function to do the combined effect to CSS, it would look something like:

function highlightResult($str, $keyword) {
    return str_replace($keyword, '<strong class="destaque">' . $keyword . '</strong>', $str);
}

The str_replace looks for all places with words queried within string and adds the <strong class="destaque"> tag, so we can apply an effect using CSS

Here is an example usage:

<?php
function highlightResult($str, $keyword) {
    return str_replace($keyword, '<strong class="destaque">' . $keyword . '</strong>');
}

if (isset($_GET['consulta'])) {//Presumindo que o nome da variável seja consulta
    $consulta = $_GET['consulta'];
    $sth = $dbh->prepare('
        SELECT * FROM tabela WHERE NOME LIKE ? OR EMAIL LIKE ?
    ');
    $busca = array('%' . $consulta . '%', '%' . $consulta . '%');
    $sth->execute($busca);

    while ($linha = $sth->fetch(PDO::FETCH_ASSOC)) {
        echo '<tr class="odd gradeX">';
        echo '<td>',  highlightResult($linha['NOME'], $consulta), '</td>'; //Adiciona efeito de destaque na primeira coluna
        echo '<td>',  highlightResult($linha['EMAIL'], $consulta) '</td>'; //Adiciona efeito de destaque na segunda coluna
        echo '<td>',  $linha['LOGIN'], '</td>';
        echo '<td>',  $linha['DT_NASCIMENTO'], '</td>';
        echo '<td>',  $linha['DT_ULTIMOACESSO'], '</td>';
        echo '<td>',  $linha['IP'], '</td>';
        echo '</td>';
    }
} else {
    ...
}

CSS should be something like (modify as needed):

strong.destaque {/*Strong já possui um efeito parecido com o negrito*/
   color: red; /*modifique a cor conforme necessário*/
}
    
24.12.2014 / 15:27