S
S
svilkov872017-04-18 09:05:02
PHP
svilkov87, 2017-04-18 09:05:02

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":
64e8e910d8c445249daf1fbbe5e3ef1d.jpg
The plate in the database, according to which the selection is made:
a2b323ab66c74d77a10d715d468f05b0.jpg
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>

The filter itself is applied next:
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>';
}

The question is what filtering happens if all values ​​are used except for , because there is no "all" or "all" value in the DB. How to make the filter select, for example, by specifying the size - "all", and the manufacturer - "KERMA", that is, specific? Thank you.
<option value="all">все</option>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2017-04-18
@tsklab

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 question

Ask a Question

731 491 924 answers to any question