gpt4 book ai didi

php - URL 中存在多个 WHERE 语句

转载 作者:行者123 更新时间:2023-11-29 12:40:41 25 4
gpt4 key购买 nike

我有一个为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com