我正在为一个网站开发过滤和排序选项,但似乎卡住了。整个代码可以在这里看到:http://pastebin.com/pYfnLiSZ
它不是 100% 完整的......像这样的部分
if(array_key_exists('number', $sorts))
{
$issue = $sorts['number'];
$setIssue = "AND i.item_number = ?";
}
仍然需要编辑一下,但我希望它是可以理解的。
我想不通的部分是这个(pastebin 中的第 126 行):
if( !empty($userIDS) && $letter )
{
$ref = array();
foreach($userIDS as $id)
{
$ref[] = $id['followed'];
}
$ref[] = $letter;
$params = implode(",", array_fill(0, count($userIDS), "?"))."?";
$prep = implode("", array_fill(0, count($userIDS), "i"))."s";
}
我不确定如何使上面的 if
动态化。仅当 $userIDS
数组不为空且 $letter
或 $year
或 $publisher
或$number
已设置。所以基本上可以是任意组合。属于 $userIDS
数组中的用户的项目,并按字母、年份、出版商、数字以及将来可能的其他过滤器进行过滤。或者它可以是年份和出版商,或出版商、编号和年份……等等。
最佳答案
由于您正在尝试“约束或条件的组合”,我可以建议您使用我在商店中使用的以下模式:
- 定义一个类
- 制定规则
- 查询
优化缓存或其他东西
class condition { public function add($query) { $this->query[] = $query; return $this; } public function get() { return " SELECT * FROM TABLE WHERE " . implode(" and " ,$this->query); } $con = new condition(); $query = $con->add("price > 100 and price < 200")->add("year > 1980")->get();
关于PHP MYSQL - 过滤和排序功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604160/