M
M
Mokhirjon Naimov2015-07-16 20:56:02
Algorithms
Mokhirjon Naimov, 2015-07-16 20:56:02

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 {
    // здесь по умолчанию
}

And now imagine that you need to filter products by 10-15 parameters :) My head is spinning...
well, I want to optimize all this (learn about a practical solution). I'm sure many have faced this problem, share the solution.
In particular, the solution is needed for Laravel 5.1. But regular PHP & SQL would be a tank for me too :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Plisko, 2015-07-17
@zvermafia

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);
}
//....

You can optimize by putting filters in an array
foreach($filters as $filter) {
     if ($value = \Input::get($filter)) {
        $model->where($filter, $value);
    }
}

etc.
If you have a normal base with EAV and facets, then the ::has construction will be useful.
Read the framework doc so that there are no hemorrhoids due to ignorance of the work of its constructions, as I understand that where add up to and you didn’t seem to know.

D
Denis Kotlyarov, 2015-07-17
@denisandroid

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 question

Ask a Question

731 491 924 answers to any question