2014-08-28 59 views
-1

嗨,我已經可以進行搜索。現在我想如何根據他們的角色放置這些數據,基於這張圖片我想讓管理員,管理員,用戶,用戶,用戶。需要使用排序?我不知道。根據用戶角色排序我的顯示數據

my website

,這我的編碼搜索。我應該在哪裏排序?

<?php 
include 'config1.php'; 
if(isset($_POST['search'])){ 

$searchTerm = $_POST['search']; 
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%'"; 
$result = mysql_query($query); 
$count = mysql_num_rows($result); 
echo "<table height = '30%'border='1'>"; 
if($count == 0) 
    { 
    echo "NO ID REGISTERED!"; 
    } 
else 
{ 
    while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td width='5%'><b>USER ID:</b> {$row['userid']} </td>"; 
    echo "<td width='5%'><b>USER NAME :</b> {$row['username']} </td>"; 
    echo "<td width='5%'><b>USER EMAIL:</b> {$row['useremail']} </td>"; 
    echo "<td width='5%'><b>USER ROLE:</b> {$row['userrole']} </td>"; 
    echo "<td width='5%'><b>USER DIVISION:</b> {$row['userdiv']} </td>"; 
    echo "<td width='5%'><b>USER DEPARTMENT:</b> {$row['userdepartment']} </td>"; 
    echo "</tr>"; 
    } 
} 

echo"</table>"; 
} 
?> 
+2

使用'ORDER BY上的查詢userrole',並考慮開溝'mysql_'功能,並與'mysqli_ *'或'PDO'與替換它們的方法制得的語句(http://stackoverflow.com/questions/12859942/why-shouldnt -i-use-mysql-functions-in-php/12860140#12860140) – Ghost 2014-08-28 07:14:20

+0

我應該在哪裏放ORDER BY? – lightyagami 2014-08-28 07:14:54

+0

你能給我完整的編碼嗎? – lightyagami 2014-08-28 07:15:10

回答

0
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' order by fieldWhichYouWant"; 

OR

$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole ASC"; 

或添加ASCDESC爲升序或降序奧德進行排序。

替換fieldwhichyouwant與您的要求。

+0

非常感謝你的幫助:') – lightyagami 2014-08-28 07:18:16

3
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole"; 

也可以使用ASC或DESC在ORDER BY後以升序或降序顯示。 例如。

Select * from users WHERE choice = 'PHP' ORDER BY id ASC; 
+0

謝謝,它的工作原理:') – lightyagami 2014-08-28 07:22:31

+0

@PuzzledBoy:好的。 :) 謝謝 ! – Naincy 2014-08-28 07:23:23

+0

是啊,這是正確的:) @PuzzledBoy – Naincy 2014-08-28 07:24:33

相關問題