实现类别过滤器的正确方法

发布于 2024-12-23 02:41:53 字数 1055 浏览 3 评论 0原文

我真的很困惑应该如何处理/实现过滤器功能。

所以我是一个简单的复选框,有 4 个值:val1、val2、val3、val4,它必须充当类别过滤器。如果用户选择 val1 和 val3 将仅显示该类别的结果。

好吧,问题是我应该如何在后端实现这个功能。我认为最好的想法是使用 switch 函数,但问题是 $filterData 带来以下格式的值:val1,val3,val4,因此 switch 和 case 可以工作。

我正在考虑尝试使用“,”作为粘合剂来将其内爆,但从我的尝试来看,直到现在我才成功。

那样工作

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

它应该像我尝试做的

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

,但问题是这将返回:

Array
(
    [value1Results] => value1
)

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

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

任何想法如何实现/修复这个?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她比我温柔 2024-12-30 02:41:53

如果我理解正确的话,你必须在 switch 中指定每个 val# 并使用 switch($key) 而不是 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
}
}

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
}
}
慕巷 2024-12-30 02:41:53

实现类别过滤器的正确方法通常是在数据库级别使用 In 语句。

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

如果您确实必须在 PHP 级别执行此操作,我建议使用迭代器/循环来检查每个结果,例如 http://php.net/manual/en/function.in-array.php 在你的集合中并将其添加到一个新数组中。这比在数据库级别执行要慢得多。

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.

野心澎湃 2024-12-30 02:41:53

如果我理解这个问题,问题就在于在 switch 语句中,条件仅计算一次,并将结果与​​每个 case 语句进行比较。因此, switch 语句不符合您的要求,因为它只是评估第一个匹配值(正如您提到的,这就是您所发生的情况)。相反,只需在 foreach 循环中使用一个简单的 if ,如下所示:

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

}

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

}
过期情话 2024-12-30 02:41:53

好的,这是适合我的解决方案

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

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;
             }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文