Search data in BD when loading page

Asked

Viewed 37 times

0

I am creating a search site and I have some links with certain categories. I need a system where by clicking these categories the loaded page can fetch the data in the BD and display.

I know it is necessary to put the action on the link, but I do not know how.

  • And what result you search for, a listing of products of these categories?

1 answer

0


For you to make this query, you can do via GET that for this form of query is one of the most used.

First you need to determine an ID for each category, so each link will have a href for php list. and your GET category will have an ID, thus getting the HTML part:

<a href="lista.php?categoria=1">Categoria 1</a>
<a href="lista.php?categoria=2">Categoria 2</a>
<a href="lista.php?categoria=3">Categoria 3</a>
<a href="lista.php?categoria=4">Categoria 4</a>

In the archive php list. will be responsible for receiving the code of the category and show the items that are linked to this category, getting something like this:

<?php
//recupera o ID da categoria passado pelo GET
$idCategoria = $_GET['categoria'];

//consulta dos itens vinculados com a categoria informado no GET
$stmt = $pdo->prepare("SELECT * FROM itens WHERE categoria = :idCategoria");
$stmt->execute(['idCategoria' => $idCategoria]); 
$itens = $stmt->fetch();

var_dump($itens); //resultado da sua consulta.

Browser other questions tagged

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