Filter system by category

0

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

I'd like to add the filter option to category followed by a _<input type="text">_ for the user to select the category and type 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 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 along with the category filter):

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

Example : User chose to filter by title and when he clicked on the letter "A" search within the category and display the results of the titles that he ate with the selected letter.

    
asked by anonymous 13.07.2018 / 14:00

1 answer

1

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

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

I'll do 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);

It's basically this, I do not know how your back-end is, the question of searching for letters, you 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, please use the guide for reference.

If you have not understood like, please read this explanation .

I hope I have succeeded in giving you a light on how to do this. 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 database, but here is a generic example.

    
13.07.2018 / 15:31