如何重新排列表的数据顺序?

时间:2016-02-10 05:04:45

标签: php html database

我已将我的网站上传到互联网。但我需要更改输入数据的顺序到我的表。新记录显示在表格的底部,而我需要它在顶部。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

请使用按(id)

排序

当您更新或创建日期时,请使用按(created or updated)日期排序。这将解决您的问题。

排序依据您的查询中的order by

例如:

ORDER BY column_name DESC

答案 1 :(得分:0)

要在顶部显示最新信息,您需要按降序订购设置结果。

但是要按降序排序(要获得最新的排序),您需要在表格中使用整数值(可能是您的主键)或日期

这是一个例子。

假设我的数据库中有一个名为Events的表,这里是表的结构

enter image description here

现在我要显示没有排序的结果,这意味着现在id = 7的结果应显示在顶部。这是

的php代码
<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 column_name DESC

在顶部显示最新结果