我有一个为RSS提要创建的PHP页面。这为学校拉出了课程。
在URL中,我添加了locationid (按位置过滤)和course parent (按学校过滤)。
当前的网址结构是index.php?locid=1&tsid=2 (ts =培训网站) THis拉出所有具有locationid=1的课程,并且培训网站id =2的效果很好。
问题是,现在我们需要能够从URL中提取两个位置,所以locid=1 OR =2我在考虑添加另一个变量(?locid=1&locid2=2),但在这个查询中我被限制为两个位置。即使我如何让它足够智能,如果有两个WHERE (locationid=1或locationid=2),我也可以使用括号
有没有一种聪明的方法来做到这一点呢?
HEre是代码。
$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";提前谢谢你!
发布于 2014-10-02 21:33:05
你也可以在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";发布于 2014-10-02 21:27:48
您还可以使用分隔符分隔您的ID,如',‘,因此您的URL如下所示:
index.php?locid=1,2,3&tsid=4,6,234然后您可以在PHP中使用explode()将其分解为一个数组,并将其用于您的请求。
$locidArray = explode(',', $_GET['locid']);发布于 2014-10-02 21:30:57
如果您的URL是这样的:
index.php?locid[]=1&locid[]=2你会在$_GET['locid']脚本中得到一个值为1,2的数组,并可以使用它:
foreach($_GET['locid'] as $locid) {
echo $locid.'<br>';
}要在mysql查询中使用:
$where = '`locid` IN ('.implode(',', $_GET['locid']).')';https://stackoverflow.com/questions/26161874
复制相似问题