2014-10-02 94 views
0

我有一个为RSS提要创建的PHP页面。 这拉了一所学校的课程。URL中的多个WHERE语句

在URL中,我添加了locationid(按位置筛选)和course parent(按学校过滤)。

URL结构是目前的index.php?LOCID = 1 & TSID = 2(TS =培训网站) 这个拉已经locationid = 1的所有课程和培训现场ID = 2 运行完美。

的问题是,现在我们需要能够拉从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 coursedate >= CURRENT_DATE() AND privatecourse='no' AND $where"; 

预先感谢您的WHERE语句!

+0

,如果我正确地理解你的问题,你想在配置参数是数字NUM的基地动态创建MySQL查询。我对么? – 2014-10-02 13:30:27

+0

房间里有一只看不见的大象:SQL注入!大声哭泣,为什么大家都想立即破解OP的网站?至少有一点SQL注入就足够了,例如通过检查所有id是否真的是数字。 – nalply 2014-10-02 13:41:30

回答

0

您还可以通过一个分隔符,像独立的标识“”让你的URL看起来像:

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

然后你就可以在PHP爆炸()到一个数组中爆发,并使用此为您请求。

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

在这种情况下使用数组访问表示法会更好。 – 2014-10-02 13:32:47

+0

这一切都取决于你想要你的URL有多美丽,数组访问符号可能更正确,但看起来不太好。这并不太复杂。 – Matthias 2014-10-02 13:38:32

+0

@MikeBrant,不,我不认为它“好多了”。这只是其中一种可能的方式。 – nalply 2014-10-02 13:39:07

0

如果你把你的网址这样的方式:

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

你会得到PHP脚本$_GET['locid']与值[1,2]数组,可以使用它:

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

用于mysql查询:

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

我会使用数组访问表示法通过get来传递数据数组。这可能是这样的:

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

PHP会自动拍摄同名的参数与[][foo]符号,并将其组装成在你的情况下,参数名$_GET['locid']访问的阵列。

+0

并且不要忘记验证数据以避免SQL注入。 – nalply 2014-10-02 13:43:00

+0

@nalply事实上,这也需要在这里完成。 – 2014-10-02 13:44:27

0

您可以在GET中创建本地而不是现在的单个值。例如:

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 .""; 
    } 
} 
0

在检查参数你循环时,你可以用一组ID像

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

,然后GET请求array

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 .""; 
} 
} 
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"; 
0

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

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; 
    } 
}