jeudi 17 septembre 2015

sql checkbox and mulit select filtering

i'm having some problems filtering my users when a search is run. It has to be possible to select more than one region and gender. gender is checkboxes and region is a select multiple selector.

In my project all content is dynamic, but thats to much to show here.

the form:

<form action="" method="get">

    <input type="checkbox" name="gender[]" value="1"> <!-- male -->
    <input type="checkbox" name="gender[]" value="2"> <!-- female -->

    <select name="region[]" multiple>

        <option value="1">North</option>
        <option value="2">East</option>
        <option value="3">West</option>
        <option value="4">South</option>

    </select>

    <input type="submit" name="submitSearch" value="Filter">
</form>

The filter function:

<?php

    if(isset($_GET['submitSearch']){
        user_filter($db);
    }

    function user_filter($db){

        $gender = $_GET['gender'];
        $region = $_GET['region'];

        $sql = "SELECT name, region, img FROM users WHERE true $gender AND $region";
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $res = $stmt->fetchAll();
        return $res;

    }

?>

Not sure if i have do a loop with the arrays and there is a problem with WHERE true if nothing is set

hope somebody can help me Thanks




Aucun commentaire:

Enregistrer un commentaire