Search field in a table with PHP

3

I created a frame on the intranet through WordPress (php, MySQL) to list the extensions of company employees. Clicking the link of extensions displays a table and the scroll bar for viewing the list. I need to create a search field (field created but with no functionality yet) on this page so that the user has the option of instead of looking for the list whom she wants, she type the name of the person, click search and when doing this the table if you restrict only people with the name or part of the name that matches the person you entered. How to do this? 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>
    
asked by anonymous 04.02.2015 / 19:00

1 answer

3

Apparently you're 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>

While using mysql_real_escape_string to avoid SQL injections, it is best to use PDO for this type of interaction.

    
08.02.2015 / 00:24