Search field in a table with PHP

Asked

Viewed 1,334 times

3

I created a frame on the intranet via Wordpress (php, Mysql) to list the employee extensions of the company. By clicking on link of extensions appears a table and the scroll bar to view the list. I need to create a search field (field created but no functionality yet) on this page so that the user has the option of instead of searching for the list who he wants, he type the person’s name, click search and when doing this the table restricts only people with the name or part of the name corresponding to the typed. How do you do that? Follow me current code.

<!-- Campo para pesquisa -->
<input type="text" name="texto"  placeholder="Digite a pesquisa" />
<button type="submit">Buscar</button></div><br><br>

<?php
   $sql = "SELECT ramal, setor , coalesce (funcionario, setor) as funcionario FROM ramais ORDER BY funcionario asc";
   $dados = mysql_query($sql); 

   $linha = mysql_fetch_array($dados);
?>

<table class="lista-ramais">

<?php 
   $ramal = ($linha['ramal']);
   $setor = ($linha['setor']);
   $funcionario = ($linha['funcionario']);
   $classe = "";
   if ( $linha['ramal'] <> " " ) { $classe=" style='background:#E0DEFE'"; 
     echo "</table><br /><table class='lista-ramais'><tr><th " . $classe . ">Usuário</th><th " . $classe . ">Ramal</td> <th " . $classe . ">Setor</th></tr>";

     while ( $linha = mysql_fetch_array($dados) ) {
        echo "<tr><td align=left>" . ucwords($linha['funcionario']) . "<br /></td><td>" . ucwords($linha['ramal']) . "</td> <td>" . ucwords($linha['setor']) . "</td></tr>";
     }
   }    
 ?>
</table>

1 answer

3


Apparently you are not using Wordpress for anything on this page.

So just create a simple filter in your mysql query:

<!-- Campo para pesquisa -->
<input type="text" name="texto"  placeholder="Digite a pesquisa" />
<button type="submit">Buscar</button></div><br><br>

<?php
   $sql = "SELECT ramal, setor , coalesce (funcionario, setor) as funcionario FROM ramais"; //inicio do sql
   $busca = mysql_real_escape_string($_POST['texto']); //sanitiza e carrega os dados do form
   if (!empty($busca)) { 
        $sql .= " WHERE funcionario LIKE %" . $busca ."%";
   }
   $sql .= " ORDER BY funcionario asc"; //fim do sql. importante manter o espaço no início
   $dados = mysql_query($sql); 
   $linha = mysql_fetch_array($dados);
?>

<table class="lista-ramais">

<?php 
   $ramal = ($linha['ramal']);
   $setor = ($linha['setor']);
   $funcionario = ($linha['funcionario']);
   $classe = "";
   if ( $linha['ramal'] <> " " ) { $classe=" style='background:#E0DEFE'"; 
     echo "</table><br /><table class='lista-ramais'><tr><th " . $classe . ">Usuário</th><th " . $classe . ">Ramal</td> <th " . $classe . ">Setor</th></tr>";

     while ( $linha = mysql_fetch_array($dados) ) {
        echo "<tr><td align=left>" . ucwords($linha['funcionario']) . "<br /></td><td>" . ucwords($linha['ramal']) . "</td> <td>" . ucwords($linha['setor']) . "</td></tr>";
     }
   }    
 ?>
</table>

Despite using the mysql_real_escape_string to avoid injections of SQL, the most appropriate is to use PDO for this type of interaction.

Browser other questions tagged

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