Filter system by category

Asked

Viewed 562 times

0

I wanted to make a search system where I would search data typed according to the selected category, but I do not know where to start.

I’d like to add the option of filter for category followed by a _<input type="text">_ for the user to select the category and write what he wants to search for :

<select name="opcao_filtro">
           <option value="nulo">--</option>                
           <option value="titulo">Título</option>
           <option value="autor">Autor</option>
           <option value="tema">Tema</option>
           <option value="editora">Editora</option>
           <option value="indice">Índice</option>
</select>

Just below would have the input:

<input type="text" name="busca" id="busca">
<input type="submit" value="Procurar">

And then (optional for the user) search for letters (would work together with the category filter):

<a href="url">A</a>
<a href="url">B</a>

Example: Useario chose to filter by title and when he clicks on the letter " To " searched between the category and displayed the results of the titles that started with the selected letter.

1 answer

1

You could add the filters to the url, that is, you can play your select inside the form.

On the page that will receive the request could do something like:

I’ll do it with PDO

$db = new PDO('mysql:host=localhost;dbname=testdb;','username','password');
$categoria = $_GET['opcao_filtro'];
$stmt = $db->prepare("SELECT * FROM table AS t WHERE t.categorias = ?");
$stmt->execute($categoria); //SE FOSSE 2 PARAMETROS, PASSE UM ARRAY
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

That’s basically it, I don’t know how your back-end is, the question of searching for letters, you’d just have to change the query to something like:

$stmt = $db->prepare("SELECT * FROM table AS t WHERE t.produtos LIKE ?");
$stmt->bindValue(1, "%$letra", PDO::PARAM_STR);

If you have questions about the PDO, use the guide for reference.

If you don’t understand the like, read this explanation.

I hope I’ve been able to shed some light on how to do that. I can not explain a little better, because I do not know if you are using framework or not, what you are using to bridge the gap between programming and bank, but here is a generic example.

Browser other questions tagged

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