作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个为 RSS 源创建的 PHP 页面。这将为学校拉取类(class)。
在 URL 中,我添加了 locationid(按位置过滤)和类(class)家长(按学校过滤)。
URL 结构当前为index.php?locid=1&tsid=2(ts = 培训站点)这将提取 locationid=1 且培训站点 id=2 的所有类(class)作品完美。
问题是现在我们需要能够从 URL 中提取两个位置 SO locid=1 OR =2我正在考虑添加另一个变量 (?locid=1&locid2=2) 但随后我仅限于此查询中的两个位置。即使如何使其足够智能,可以在有两个 WHERE (locationid=1 OR locationid=2) 的情况下使用括号
有没有一种明智的方法可以做到这一点?
这是代码。
$where = array();
if (isset($_GET['tsid'])) {
$trainingsite = $_GET['tsid'];
$where[] = 'scheduledcourses.courseparent = '. $trainingsite ."";
}
if (isset($_GET['locid'])) {
$locationid = $_GET['locid'];
$where[] = 'scheduledcourses.courselocationid = '. $locationid ."";
}
if(count($where) >= 0) { array_unshift($where,""); }
$where = implode(" AND ", $where);
$where;
这是查询中的 WHERE 语句
WHERE coursedate >= CURRENT_DATE() AND privatecourse='no' AND $where";
提前谢谢您!
最佳答案
您也可以在 URL 参数中使用数组。
例如:
http://yourdomain.com/?locid[]=1&locid[]=2
这将导致:
print_r ( $_GET['locid'] );
Result:
array( [0] => 1 [1] => 2 )
那么你就得到了(仅作为示例。编辑此内容以满足您的需要):
if( is_array( $_GET['locid'] ) && !empty( $_GET['locid'] ) ){
$where = "";
foreach( $_GET['locid'] AS $locid) {
$where .= "AND locid = '$locid' ";
}
}
$sql = "SELECT * FROM table WHERE something = something $where";
关于php - URL 中存在多个 WHERE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26161874/
我是一名优秀的程序员,十分优秀!