2016-02-10 68 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