Printing Results in a Simple Table
I have a MySQL table with a structure similar to this:
id1 id2 title url date
I would like to print out a simple开发者_StackOverflow中文版 table in PHP that with the following structure sorted in reverse chronological order for the most recent 10 entries (date above = date submitted) from the MySQL table:
title id2
How could I do this?
Use:
<table>
<tr>
<td>title</td>
<td>id2</td>
</tr>
<?php
$sql = "SELECT t.title,
t.id2
FROM TABLE t
ORDER BY t.date DESC
LIMIT 10";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("<tr><td>%s</td><td>%s</td></tr>", $row[0], $row[1]);
}
?>
</table>
ORDER BY t.date DESC
will list the most recent date at the top. If you want the list to start with the earliest date, omit the DESC
- the default is ASC
, so you don't have to type it out.
精彩评论