2010-02-27 69 views
0

所以我有這個videosection,但我想爲用戶做一個「排序」選項avaible。PHP:以智能編碼方式整理?

排序選項:ALL(沒有WHERE SQL語句),Videoklips(WHERE SCtry = 0),SC嘗試(WHERE SCtry = 1)

現在,我知道怎麼做了 「我」 的方式。我會放在index.php文件鏈接: ?sort=video?sort=SCtry

然後做2,如果排序的視頻,如果排序sctry ,然後複製整個的index.php現在(它顯示的所有內容)到2,如果公司和那麼只需編輯SQL語句SELECT,其中WHERE SCtry ='0'on?sort = video,WHERE SCtry ='1'on sort = SCtry。

現在,我知道如何理清,但我想以更聰明的方式編碼它(當然,如果它存在的話),因爲這似乎太重複整個網站,然後只改變1行...什麼我的index.php彪

例如,我要去複製:

<?php 
$hent_meetup = mysql_query("SELECT * FROM member_film ORDER BY id DESC LIMIT 0,200") or die(mysql_error()); 
while($vis = mysql_Fetch_array($hent_meetup)) { 
?> 
+2

幾個問題:你是不是使用Smarty的模板引擎,是嗎?如果正確,請刪除smarty標籤。下一個;你到底意味着什麼重複index.php?你能舉一個你想要複製的代碼的簡單例子嗎? – 2010-02-27 15:38:42

+0

請發佈一些示例代碼,以便我們可以向您展示相同代碼的反例。 – 2010-02-27 15:39:21

+1

好吧,會做,並沒有我不使用smarty模板,我即將去除標籤現在 – Karem 2010-02-27 15:41:36

回答

2

沒有看到示例代碼,我可以告訴你這是什麼,我想一個例子做。

<? 
//all of the code before the SQL statement here... 
$sql= ' SELECT `column` from `tablename`'; //or any other SQL that is appropriate before the conditional 
if(isset($_GET['sort'])){ 
    if($_GET['sort'] == 'video'){ 
     $sql .= ' WHERE `SCtry` = 0'; 
    }elseif($_GET['sort'] == 'SCtry'){ 
     $sql .= ' WHERE `SCtry` = 1'; 
    } 
} 
$sql .= ' ORDER BY `whatever`'; //or any other SQL that is appropriate after the conditional 
//rest of code... no need for duplication 
?> 

編輯爲每個操作的請求......

+0

這是好,正是我要求..但我怎麼辦,如果我也有ORDER BY ID? – Karem 2010-02-27 15:55:48

+0

我已經編輯我的示例,因爲你已經請求 – 2010-02-27 16:03:01

+0

我會去做類似 $ sort_options = array(「video」=>「where SCtry' = 0」,...);如果(isset($ sort_options [$ _GET ['sort']]))$ sql。= $ sort_option [$ _ GET ['sort']]; 但是使用doctrine或zend_db_table之類的東西會更好用 – roman 2010-02-27 16:04:36