Filter MySQL SELECT with checkboxes

Socius Decennalis
Decennium
Joined
Aug 15, 2008
Messages
489
Reaction score
43
EDIT: I have found a solution. I simply had to edit the clause where the items were added into an array. This thread may be locked/deleted.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I have a database with several items and each item has it's own categories. The categories are marked with the category ID's, for example, 1,2,3. (Item is in category ID 1, 2 and 3)
In the div id kategoriat, I create a form with checkboxes for those categories, from which the user can select the ones they want to view.

In short, if categories are defined, the MySQL statement should search for only those items that have the given category/categories.

PHP:
<?php
    if(isset($_GET['filter'])) {
        $filter = $_GET['filter'];
    }

    // Haetaan kaupat tietokannasta
    $kaupat = $connection->prepare("SELECT * FROM wp_googlekartta ORDER BY ID ASC");
    $kaupat->execute();

    $kategoriat = $connection->prepare("SELECT * FROM wp_kategoriat ORDER BY ID ASC");
    $kategoriat->execute();
?>

    <div id="kategoriat">
        <p>Kategoriat</p>
        <form action="kartta.php" method="get">
        <?php
        foreach($kategoriat AS $kategoria) {
            echo '<input type="checkbox" value="'.$kategoria['ID'].'" id="'.$kategoria['nimi'].'" name="filter[]" /> <label for="'.$kategoria['nimi'].'">'.$kategoria['nimi'].'</label><br />';
        }
        echo '<input type="submit" value="Näytä" />';
        ?>
        </form>
    </div>
 
Last edited:
Back