2016-02-10 71 views
-1

我已經將我的網站上傳到互聯網。但我需要改變輸入數據到我的表的順序。新的記錄出現在表格的底部,而我需要它在頂部。我怎樣才能做到這一點?如何重新排列表的數據順序?

+0

在您的查詢中應用排序方式 – C2486

+2

您將需要創建一個帶有一些順序增量整數的列*(如:id:1,2,3,...)*然後使用'Order By Desc' – divy3993

回答

0

請通過(id) OR

,當你有更新或創建日期然後通過(created or updated)日期排序使用排序使用。這將解決你的問題。

排序方式order by在查詢

例如:

ORDER BY column_name DESC 
0

顯示在頂部的最新的,你需要ORDER您的搜尋結果降序設定。

但要按降序排列(要獲得最新的頂部),您需要在表格中使用整數值(可能是您的主鍵)或日期

這裏是一個例子。

假設我有我的數據庫的表中調用,事件和這裏是

enter image description here

現在我要顯示不排序的結果,這現在意味着表的結構id = 7結果應顯示在頂部。這裏是該代碼的代碼

<table class="table table-striped"> 
<?php 
require_once('../DB/connect.php'); 
$result4= mysql_query("SELECT * from events "); 
while ($row= mysql_fetch_array($result4)){ 
$e_id=$row['id']; 
$e_name=$row['ename']; 
echo'<tr><td> <img src="../imgs/settings/event5.png" class="img-responsive"></td><td><b>'.$e_name.'</b> </td>'; 
echo'<td><a button type="button" href="#'.$e_id.'" data-toggle="modal" class="btn btn-primary"> EDIT </a> 

</td> 
<td><button type="button" class="btn btn-danger"> DELETE </button> 
</tr>'; 
} 
?> 
</table> 

現在上面的代碼會給我這樣的結果。

enter image description here

你可以看到上面有我的舊記錄顯示。

現在我要更改一下我的sql查詢來顯示最新的/最新的記錄。

<table class="table table-striped"> 
<?php 
require_once('../DB/connect.php'); 
$result4= mysql_query("SELECT * from events ORDER by id DESC "); 
while ($row= mysql_fetch_array($result4)){ 
$e_id=$row['id']; 
$e_name=$row['ename']; 
echo'<tr><td> <img src="../imgs/settings/event5.png" class="img-responsive"></td><td><b>'.$e_name.'</b> </td>'; 
echo'<td><a button type="button" href="#'.$e_id.'" data-toggle="modal" class="btn btn-primary"> EDIT </a> 

</td> 
<td><button type="button" class="btn btn-danger"> DELETE </button> 
</tr>'; 
} 
?> 
</table> 

所以上面的代碼會給我這樣的結果,

enter image description here

一樣,你可以在上面顯示的最新結果通過ORDER BY列名DESC