2015-11-23 74 views
-1

我正在製作一個網站,它只顯示一個mysql數據庫中的表格,並且有10個colums,我希望能夠排序。喜歡上下,或者我該怎麼說。我有一個代碼allready你可以看到,但它只有一種方法是可以做出和容易排序2種方式?如果有的話可以有人幫助我嗎?PHP mysql對兩種方式進行排序

我很新的PHP

感謝您的所有答案。

$sql = "SELECT * FROM server1"; 
if(!isset($_GET['sort'])) { 

} 
elseif ($_GET['sort'] == 'id') 
{ 
    $sql .= " ORDER BY ID"; 
} 
elseif ($_GET['sort'] == 'vsite') 
{ 
    $sql .= " ORDER BY vsite"; 
} 
elseif ($_GET['sort'] == 'registrar') 
{ 
    $sql .= " ORDER BY registrar"; 
} 
elseif ($_GET['sort'] == 'eier') 
{ 
    $sql .= " ORDER BY eier"; 
} 
elseif ($_GET['sort'] == 'ns') 
{ 
    $sql .= " ORDER BY ns"; 
} 
elseif ($_GET['sort'] == 'a') 
{ 
    $sql .= " ORDER BY a"; 
} 
elseif ($_GET['sort'] == 'mx') 
{ 
    $sql .= " ORDER BY mx"; 
} 
elseif ($_GET['sort'] == 'flyttet') 
{ 
    $sql .= " ORDER BY flyttet"; 
} 
elseif ($_GET['sort'] == 'slettet') 
{ 
    $sql .= " ORDER BY slettet"; 
} 
elseif ($_GET['sort'] == 'delt') 
{ 
    $sql .= " ORDER BY delt"; 
} 
elseif ($_GET['sort'] == 'behkd') 
{ 
    $sql .= " ORDER BY behkd"; 
} 
elseif ($_GET['sort'] == 'varenr') 
{ 
    $sql .= " ORDER BY varenr"; 
} 
elseif ($_GET['sort'] == 'server') 
{ 
    $sql .= " ORDER BY server"; 
} 
elseif ($_GET['sort'] == 'sist') 
{ 
    $sql .= " ORDER BY sistoppdatert"; 
} 
+0

只需爲「ASC」或「DESC」創建另一個字段。雖然我建議你重新考慮整個事情,並使用JS – sinaza

回答

0

能夠表格排序是沒有必要這麼長的代碼查詢後,所以我又寫道像這樣:

<?php 
if(!isset($_GET['field'])) $field = 'ID'; else $field = $_GET['field']; 
if(!isset($_GET['sorting'])) $sort = 'ASC'; else $sort = $_GET['sorting']; 


$sql = "SELECT * FROM server1 ORDER BY " .$field." ".$sort; 

if($sort == 'ASC') $sort = 'DESC'; else $sort = 'ASC'; 
?> 
0

HM只需添加ASC或DESC這樣

$sql .= " ORDER BY server DESC"; 
+0

這總是會排序desc(這是怎麼回事?) – davejal

+0

它可能與設置如果else條件如果1排序ASC和其他0排序DESC機智幫助jquery – jay