简体   繁体   中英

The right way to implement a category filter

I am really confused about how I should approach/implement a filter feature.

So I am a simple checkbox with 4 values: val1, val2, val3, val4 which has to act like a category filter. If user selects val1 and val3 will show results only from that category.

Ok so the problem is how should I implement this feature in the backend. I think the best idea would be to use a switch function but the problem is that $filterData brings values in this format: val1,val3,val4 so a switch and a case would work.

I was thinking of trying to implode it using ',' as a glue but from what I have tried I didn't succeed until now.

It should work something like this

switch ($filterData)
{
      case 'Values of filter data' (ex: val3 and val4 ):
          $result[$value.'Results'] = $this->_$value($data);
      break;
}
return $result;

I have tried to do a

$filterData = implode(',',$filterData);
foreach ($filterData as $key => $value)
{
     switch ($value){
         case $value:
              $result[$value.'Results'] = $this->_$value($data);
         break;}
}

but the problem is that this will return this:

Array
(
    [value1Results] => value1
)

Array
(
    [value1Results] => value1
    [value2Results] => value2
)

Array
(
    [value1Results] => value1
    [value2Results] => value2
    [value3Results] => value3
)

Any ideas how to implement/fix this?

If I understand corretly you have to specify each val# in switch and use switch($key) instead of switch($value) ;

//page.php?val1=value1&val3=value3

$filterData = $_GET; //for example

foreach ($filterData as $key => $value)
{
     switch ($key) {
         case 'val1':
         case 'val3':
              $result[$value.'Results'] = $this->_$value($data);
         break;
         //continue
}
}

The right way to implement a category filter is usually at the database level using an In statement.

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

If you really must do this at the PHP level i suggest using an itterator/loop to check each result with something like http://php.net/manual/en/function.in-array.php in your set and add it to a new array. This will be much slower than doing it at the database level.

If I understood the question, the problem is that in a switch statement, the condition is evaluated only once and the result is compared to each case statement. Therefore, a switch statement just doesn't fit what you are asking for, because it just evaluates the first matching value (which is what happens to you, as you mention). Instead, just use a simple if inside a foreach loop, sth like this:

//initialize result
foreach ($filterData as $key => $value)
{
   if ($key)=='val1':
   //append this category results to the result

}

Ok, here is the solution that works for me

$filterData = explode(',', $parameters['filter']);
     foreach ($filterData as $key => $value)
     {
             switch($value)
             {
                 case $value:
                     $value = '_'.$value;
                     $searchResults[$value] = $this->$value($parameters);
                 break;
             }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM