im trying to do a product filter in php via checkboxes, basically i could get it to display the products with one filter, but i am lost in how to make multiple checkboxes to work with eachother.
here's what i've tried:
the part where it changes the sql command
if (isset($_GET['checkDress'])){
if (empty($where)){
$where=" where category = 'kleit'";
}elseif(!empty($where)){
$where.=" and category = 'kleit'";
}
}
if (isset($_GET['nightDress'])){
if (empty($where)){
$where=" where category = 'oKleit'";
}elseif(!empty($where)){
$where.=" and category = 'oKleit'";
}
}
the sql command :
$res=mysql_query("select * from items $where $filter");
checking one of the checkboxes is fine, it returns the correct result, but checking 2 at the same time doesn't return anything at all. Any help would be very appreciated.
Since it seems like the dresses cannot satisfy multiple categories simultaneously, it seems like you want to use or
for the filter rather than and
. That would be a simple fix, but I think you can improve on this solution a lot.
<!-- pass filters as array -->
<input name="filter[]" type=checkbox value=kleit>
<input name="filter[]" type=checkbox value=oKleit>
$filters = $_GET['filters'];
$params = array_fill(0, count($filters), '?');
if ($params) {
$where = "WHERE category IN (" . implode(',', $params) . ")";
}
something like this?
$where = " WHERE ";
$conditions = array();
if (isset($_GET['checkDress'])){
$conditions[] = "category = 'kleit'";
}
if (isset($_GET['nightDress'])){
$conditions[] = "category = 'oKleit'";
}
$where .= implode($conditions, ' AND ');
Maybe use IN
instead of multiple AND
,, or you may have different conditions, filters, etc...