Answer the question
In order to leave comments, you need to log in
How to set up a selection of products by filter?
Good afternoon!
There is a filter on the site, which should filter by the parameters: "size" and "brand":
The plate in the database, according to which the selection is made:
The selection itself:
//выбираем товар
$st = $pdo->query('SELECT * FROM `brick`');
$oblBricks = $st->fetchAll();
//рез-т получаем в тегах select, option
<form action="" method="post">
<p class="lable">размер</p>
<select name="obl_size">
<option value="all">все</option>
<?php foreach ($oblBricks as $item): ?>
<option value="<?php echo $item['sz']; ?>"><?php echo $item['sz']; ?></option>
<?php endforeach; ?>
</select>
<p class="lable">производитель</p>
<select name="obl_brand">
<option value="all">все</option>
<?php foreach ($oblBricks as $item): ?>
<option value="<?php echo $item['brand']; ?>"><?php echo $item['brand']; ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="go" name="go_filter">
</form>
if(isset ($_POST['go_filter'])){
$size = $_POST['obl_size'];
$brand = $_POST['obl_brand'];
$stm = $pdo->prepare('SELECT * FROM `brick` WHERE sz=:sz AND brand=:brand ');
$stm->bindParam(':sz', $size, PDO::PARAM_INT);
$stm->bindParam(':brand', $brand, PDO::PARAM_INT);
$stm->execute();
$Filter = $stm->fetchAll();
// echo '<pre>';
// var_dump($Filter);
// echo '</pre>';
}
<option value="all">все</option>
Answer the question
In order to leave comments, you need to log in
all, because there is no "all" or "all" value in the DB.Because and :sz can be all
SELECT * FROM `brick` WHERE ( sz=:sz OR :sz = 'all' ) AND ( brand=:brand OR :brand = 'all' )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question