change word properties in php search

Asked

Viewed 362 times

1

personal speech, is the following I have a search script with php, I would like when it displays the result the properties of the searched word are changed(background, size, etc), for example I search the user name 'Joao' the system returns me a table with the relative 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 I should only do this with php, or I should use javascript, remembering that I am not asking for any code and yes the "best way"

follows the code of how the results of the query 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 above code generates the results of this table exemplo

  • Honestly, I couldn’t understand what you mean by background? You want to highlight the words?

  • I really think I expressed myself badly, and basically this as it happens in duckduckgo.com when you research something it leaves on bold the term

  • Which variable is received by the PDO "querie"? Provide more complete code, there is no way to know what needs to be modified. Read this please: http://answall.com/help/mcve

  • William an image does not help, because the problem is not understanding what do you want, but rather try understand what you’ve already done, because you just put a piece of code. See if my answer helps you

  • Good afternoon, did my answer help you? Please let me know if you still have any questions. if not and the answer was helpful, please mark as "correct". Grateful.

  • 1

    opa @Guilhermenascimento sorry for the delay in marking as correct had forgotten, thanks for the help Chara

  • 1

    Glad I could help, wish you and your Chara projects success. + 1 for you

Show 2 more comments

1 answer

6


Suppose you use a query like this 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 make the effect combined with the CSS, it would be something like:

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

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

Follow an example of use:

<?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 look something like this (modify as needed):

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

Browser other questions tagged

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