0

你好我想在我的网站上为搜索引擎编写一个过滤器。现在我认为这将是使用简单过滤器的最佳方式。所以我的代码看起来像:

}else if ($filter_a === 1){
        $filter = "WHERE `a` LIKE '%$a%'";
}else if ($filter_b === 1){
        $filter = "WHERE `b` LIKE '%$b%'";
}else if ($filter_c === 1){
        $filter = "WHERE `c` LIKE '%$c%'";
...
}else if ( ($filter_a === 1) && ($filter_b === 1) ){
        $filter = "WHERE `a` LIKE '%$a%' AND `b` LIKE '%$b%'";
}else if ( ($filter_a === 1) && ($filter_c === 1) ){
        $filter = "WHERE `a` LIKE '%$a%' AND `c` LIKE '%$c%'";
... and so on

问题是我有 5xN所以这个排列将是 120 possibilites$filter

有没有办法解决这个问题?如果有人可以帮助我,我真的很感激。多谢。

4

1 回答 1

2

这条路怎么样?

if ($filter_a === 1) {
        $filter[] = "`a` LIKE '%$a%'";
}
if ($filter_b === 1) {
        $filter[] = "`b` LIKE '%$b%'";
}
if ($filter_c === 1) {
        $filter[] = "`c` LIKE '%$c%'";
}
$filter = "WHERE ".implode(" AND ", $filter);
于 2012-04-23T09:50:09.760 回答