S
S
SirieS2020-05-21 23:27:28
PHP
SirieS, 2020-05-21 23:27:28

How to set up auto-completion of a product template in PHP from the database (via PDO)?

The task is to set up a product card php template so that it is filled from the database. Those. when you click on a product of interest in the catalog, a product card template opens, which is filled from the database. The base is connected via PDO. Help me figure out how to pass information about which product is selected to the template in order to make a selection, for example, by id from an array.
My catalog page is configured like this:
(lines from the database file)

$sql = 'SELECT * FROM goods';
$statement = $pdo->query($sql);
$goods = $statement->fetchAll(PDO::FETCH_ASSOC);

(strings from directory)
<?php
   foreach($goods as $good_itm) {
?>

<div class="product">
   <a href="/pages/product
<?php echo $good_itm['id']?>.php"><img src="/img/
<?php echo $good_itm['image']; ?>
"></a>
</div>

<?php
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Kalinin, 2020-05-22
@MaximAr1es

I assume that for each product that should be filled in, you need to pass an identifier, after which you make a selection by this identifier and fill it with data.

A
Adik Izat, 2020-05-22
@JaxAdam

$id = {id товара};
$statement = $pdo->prepare("SELECT * FROM goods WHERE id = ?");
$statement->execute([$id]);
$good = $statement->fetch(PDO::FETCH_ASSOC);

product id can be taken from url.
If $good != null then you show the product, if there is no data about the product, you show 404 or that there is no such product.
I also advise you to use bind_value (), it is in PDO, to avoid SQL injection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question