Всем привет !
Делаю фильтрацию через get параметры (использую RedBeanPHP) :
//sort
$sort = $_GET["_sort"];
//order
$order = $_GET["_order"];
//limit
$limit = $_GET["_limit"];
//search
$search = $_GET["q"];
//category
$category = explode(",", $_GET["category"]);
//min - max
$min = $_GET["min"];
$max = $_GET["max"];
//auth
$auth = explode(",", $_GET["auth"]);
$query = "";
$query .= isset($sort) && isset($order) ? "ORDER BY $sort $order " : "";
$query .= $limit !== 0 ? "LIMIT $limit " : "";
$query .= isset($search) ? "search_tags LIKE '%$search%'" : "";
if ($category[0] !== "") {
foreach ($category as $categoryKey => $value) {
if ($categoryKey === 0) {
$query .= "category LIKE '%$value%' ";
} else {
$query .= "OR category LIKE '%$value%' ";
}
}
}
$query .= $min !== 0 && $max !== 0 ? "price >= $min AND price <= $max " : "";
if ($auth[0] !== "") {
foreach ($auth as $authKey => $value) {
if ($authKey === 0) {
$query .= "auth LIKE '%$value%' ";
} else {
$query .= "OR auth LIKE '%$value%' ";
}
}
}
$goodsMV = R::findAll("goods", $query);
Как не городить большое количество if и правильно указывать SQL, то есть OR, AND?