我正在显示mySQL数据库中的数据表(由填写表单的用户存储)。如何显示它以便提交的最新数据位于页面顶部?
这是我从表中检索数据并显示它的代码。目前,我可以通过姓名,电子邮件等方式安排表格。
<?php
// connect to the database
require 'connect.php';
if(isset($_GET['orderby']))
{
$orderby = $_GET['orderby'];
$query = "SELECT name_first,name_last,email,company,city,state,phone,session,optout FROM table
ORDER by ".mysql_real_escape_string($orderby)."";
}
else
{
$query = "SELECT name_first,name_last,email,company,city,state,phone,session,optout FROM table";
}
$result = mysql_query($query) or die(mysql_error());
?>
<table style='width: 60%; text-align: left;' cellpadding = '1'>
<tr bgcolor=#000090>
<th><a href=prospstud.php?orderby=name_first><FONT COLOR = #FFFFFF><b>First</b></th>
<th><a href=prospstud.php?orderby=name_last><FONT COLOR = #FFFFFF><b>Last</b></th>
<th><a href=prospstud.php?orderby=email><FONT COLOR = #FFFFFF><b>Email</b></th>
<th><a href=prospstud.php?orderby=phone><FONT COLOR = #FFFFFF><b>Phone</b></th>
<th><a href=prospstud.php?orderby=session><FONT COLOR = #FFFFFF><b>Session</b></th>
<th><a href=prospstud.php?orderby=company><FONT COLOR = #FFFFFF><b>Company</b></th>
<th><a href=prospstud.php?orderby=city><FONT COLOR = #FFFFFF><b>City</b></th>
<th><a href=prospstud.php?orderby=state><FONT COLOR = #FFFFFF><b>State</b></th>
<th><a href=prospstud.php?orderby=optout><FONT COLOR = #FFFFFF><b>Opted Out?</b></th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result))
{
$name_first = $row['name_first'];
$name_last = $row['name_last'];
$email = $row['email'];
$phone = $row['phone'];
$session = $row['session'];
$company = $row['company'];
$city = $row['city'];
$state = $row['phone'];
$optout = $row['optout'];
echo "<tr bgcolor=#ABB5F6>
<td>$name_first</td>
<td>$name_last</td>
<td>$email</td>
<td>$phone</td>
<td>$session</td>
<td>$company</td>
<td>$city</td>
<td>$state</td>
<td>$optout</td>
</tr>";
}
答案 0 :(得分:3)
每次访问/创建会话时,您都可以在数据库中保存时间戳值。
然后您可以使用:
ORDER BY timestampRow DESC
在您的SQL查询中。
答案 1 :(得分:0)
您可以在数据库表上放置自动递增的ID。 当您执行选择时,您可以选择具有最大ID的数据。像这样:
SELECT "id,name_first,name_last,email,company,city,state,phone,session,optout FROM table" WHERE id >=MAX(id);