$where = array();
if (isset($_GET['manufacturer']))
$where[] = "`manufacturer` = '".$mysqli->escape_string($_GET['manufacturer'])."'";
if (isset($_GET['madeIn']))
$where[] = "`madeIn` = '".$mysqli->escape_string($_GET['madeIn'])."'";
$query = "SELECT * FROM `table` WHERE ".implode(" AND ", $where);
$result = $mysqli->query($query);
$manufacturer = '';
$byManufacturer = 0;
$madeIn = '';
$byMadeIn = 0;
if (isset($_GET['manufacturer'])) {
$manufacturer = $_GET['manufacturer'];
$byManufacturer = 1;
}
if (isset($_GET['madeIn'])) {
$madeIn = $_GET['madeIn']";
$byMadeIn = 1;
}
$query = $mysqli->prepare(
"SELECT * FROM `table` ".
"WHERE (? = 0 OR `manufacturer` = ?) ".
"AND (? = 0 OR `madeIn` = ?)");
$query->bind_param('isis', $byManufacturer, $manufacturer, $byMadeIn, $madeIn);
$query->execute();
$rgFilters = $_POST['filters'];
$rgSql = [];
foreach($rgFilters as $k=>$v){
switch($k){
case 'type':
$rgSql[] = 'type_id = ' . $v;
break;
case 'country':
$rgSql[] = 'country_id = ' . $v;
break;
case 'price_from':
$rgSql[] = 'price > ' . $v;
break;
case 'price_to':
$rgSql[] = 'price > ' . $v;
break;
case 'manufacturer':
$rgSql[] = 'manufacturer_id = ' .$v;
break;
}
}
/**
* Для учета всех параметров
*/
$query = 'SELECT * FROM `products` ' . implode(' AND ', $rgSql);
/**
* Для получение товаров с совпадением хотябы одного параметра
* Конечно бредовая ситуация, ну а вдруг
*/
$query = 'SELECT * FROM `products` ' . implode(' OR ', $rgSql);
$sort_types = ['type', 'country', 'price'];
$sign_for_query = ['type' => '=', 'price' => '>', 'country' => '='];
$sql = '';
$data = $_POST['filters'];
foreach($sort_types as $type) {
if(array_key_exists($type, $data)) {
$sql[] = '`'.$type . '` ' . $sign_for_query[$type] . ' ' .$data[$type];
}
}
implode(' AND ', $sql)
$specials = ['price' => 'min({val})'];
foreach($sort_types as $type) {
if(array_key_exists($type, $data)) {
if(array_key_exists($type, $specials)) {
$sql[] = str_replace('{val}', $el, $specials[$el]);
} else {
$sql[] = '`'.$type . '` ' . $sign_for_query[$type] . ' ' .$data[$type];
}
}
}