[英]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. 所以我是一个简单的复选框,带有4个值:val1,val2,val3,val4,它必须像类别过滤器一样工作。 If user selects val1 and val3 will show results only from that category. 如果用户选择val1,则val3仅显示该类别的结果。
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. 我认为最好的主意是使用switch函数,但问题是$ filterData会以以下格式带来值:val1,val3,val4,因此可以进行开关和操作。
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)
; 如果我了解得很清楚,则必须在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
}
}
The right way to implement a category filter is usually at the database level using an In statement. 实现类别过滤器的正确方法通常是在数据库级别使用In语句。
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in http://dev.mysql.com/doc/refman/5.0/zh/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. 如果您真的必须在PHP级别上执行此操作,建议您使用itterator / loop来检查每个结果,并在您的集合中添加http://php.net/manual/en/function.in-array.php之类的内容,并将其添加到一个新的数组。 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. 如果我理解这个问题,问题是在switch语句中,条件仅被评估一次,并且将结果与每个case语句进行比较。 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). 因此,switch语句不能满足您的要求,因为它只评估第一个匹配值(正如您提到的那样,这就是您遇到的情况)。 Instead, just use a simple if inside a foreach loop, sth like this: 相反,只需在foreach循环中使用简单的if,就像这样:
//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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.