Answer the question
In order to leave comments, you need to log in
How do you optimally filter data by many parameters?
I must say right away that I need a solution that I can use in shared hosting, that is, ordinary PHP & MySQL (without Radis, etc.)!
I am making an online store, as you know, the goods must be filtered by many parameters (well, for user convenience). For example, for 3 parameters (color, size, brand), my solution is (well, just terrible):
if (isset($color) && isset($size) && isset($brand)) {
// where color = $color and size = $size and brand = $brand
} elseif (isset($color) && isset($size) && ! isset($brand)) {
// where color = $color and size = $size
} elseif (isset($size) && isset($brand) && ! isset($color)) {
// where size = $size and brand = $brand
} elseif (isset($color) && isset($brand) && ! isset($size)) {
// where color = $color and brand = $brand
} elseif (isset($color) && ! isset($size) && ! isset($brand)) {
// where color = $color
} elseif (isset($size) && ! isset($color) && ! isset($brand)) {
// where size = $size
} elseif (isset($brand) && ! isset($color) && ! isset($size)) {
// where brand = $brand
} else {
// здесь по умолчанию
}
Answer the question
In order to leave comments, you need to log in
And what's the problem, why such complex ifas, in a simple version, you can get by with simple
if ($color = \Input::get('color')) {
$model->where('color', $color);
}
if ($size = \Input::get('color')) {
$model->where('size', $size);
}
//....
foreach($filters as $filter) {
if ($value = \Input::get($filter)) {
$model->where($filter, $value);
}
}
Wow, the script is really heavy :)
So I can’t tell you, but if I used Mysql, then the usual WHERE would be enough for him.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question