2014-10-02 98 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; 
    } 
}