URL中的多个WHERE语句

时间:2014-10-02 13:24:42

标签: php mysql

我有一个为RSS源创建的PHP页面。 这为学校开设了课程。

在网址中,我添加了locationid(按位置过滤)和课程家长(按学校过滤)。

网址结构目前是index.php?locid = 1& tsid = 2(ts =培训网站) 这将拉出所有locationid = 1且训练网站id = 2的课程 工作完美。

问题是现在我们需要能够从URL SO 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";

提前谢谢!

7 个答案:

答案 0 :(得分:1)

您可以在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";

答案 1 :(得分:0)

您还可以使用分隔符分隔您的ID,例如“,”,以便您的网址如下所示:

index.php?locid=1,2,3&tsid=4,6,234

然后你可以用PHP中的explode()将它爆炸成一个数组并将其用于你的请求。

$locidArray = explode(',', $_GET['locid']);

答案 2 :(得分:0)

如果您使用这样的网址:

index.php?locid[]=1&locid[]=2

您将在$_GET['locid']中使用值为[1,2]的数组中的PHP脚本,并且可以使用它:

foreach($_GET['locid'] as $locid) {
    echo $locid.'<br>';
}

在mysql查询中使用:

$where = '`locid` IN ('.implode(',', $_GET['locid']).')';

答案 3 :(得分:0)

我会使用数组访问表示法通过get传递数据数组。这看起来像这样:

index.php?locid[]=1&locid[]=2&tsid=2

PHP将使用[][foo]表示法自动获取同名参数,并将它们组合成一个可在您的案例中以参数名$_GET['locid']访问的数组。

答案 4 :(得分:0)

您可以在GET中使locid成为一个数组,而不是现在的单个值。例如:

http://link/myids.php?locid[]=1&locid[]=2&locid[]=3

然后在if (isset($_GET['locid'])) {

$first = true;
foreach ( locid as $_GET['locid'] ) {
    if ( first ) {
        first = false;
        $where[] = 'scheduledcourses.courselocationid = '. $locationid ."";
    } else {
        $where[] = ' AND scheduledcourses.courselocationid = '. $locationid ."";
    }
}

答案 5 :(得分:0)

你可以在get请求中使用一组id,比如

index.php?locid[]=1&locid[]=2&tsid=2

然后在检查参数时循环遍历数组

if (isset($_GET['locid'])) {
if(is_array($_GET['locid']){
    $tmpOrWhere = array();
    foreach($_GET['locid'] as $locid){
        $tmpOrWhere[] = 'scheduledcourses.courselocationid = '. $locid."";
    }
    $where[] = "(" . implode (" OR " , $tmpOrWhere) . ")";
}else{
    $locationid = $_GET['locid'];
    $where[] = 'scheduledcourses.courselocationid = '. $locationid ."";
}
}

答案 6 :(得分:0)

您可以在网址中使用数组语法:

index.php?locid[]=1&locid[]=3&locid[]=7&tsid=2

然后你的php变成:

if (isset($_GET['locid'])) {
    $locationid = $_GET['locid'];
    if(is_array($locationid)){
        $where[] = 'scheduledcourses.courselocationid IN('. implode(',', $locationid) .')';
    }else{
        $where[] = 'scheduledcourses.courselocationid = '. $locationid;
    }
}