【问题标题】:Sorting PHP echo output by time posted按发布时间对 PHP 回显输出进行排序
【发布时间】:2012-11-04 00:35:54
【问题描述】:

好的,所以。我今天在 stackOverflow 上问了大约 5 个问题,你们都很有帮助。

现在,我是一名设计师,正在学习编码,所以请耐心等待。

我有一个 mySQL 表,其中有一个小的 CMS/博客正在构建。 我已经按照我现在想要的方式设计了它。 这是页面的代码。

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
    echo "<h1>" . $row['Title'] . "</h1>";
    echo "<br>";
    echo "<h2>" . "Posted on:   " . $row['Date'] . "</h2>";
    echo "<br>";
    echo "<img src='".$row['Image']."'/>";
    echo "<br>";
    echo $row['Message'];
    echo "<br>";
    echo "<br>";
}

我仍在努力,所以一切都很好。

我想知道的是,这段代码是将我的sql数据输出到一个页面中。例如,有什么方法可以告诉页面以什么顺序回显数据。在我的 SQL 表中,我有:

2012-11-03 16:16:06     This is my First Blog Post  This is the first message of the first blog post. ...   http://blog.gettyimages.com/wp-content/uploads/201.

接下来是

2012-11-03 16:17:29     This is my Second Blog Post     This is the second message of the Second Post, You...   http://www.aviation-images.com/user/zooms/118/451n...

如何让页面始终在顶部显示最新帖子,在下方显示较旧的帖子?

【问题讨论】:

    标签: php sql css html


    【解决方案1】:

    在查询中使用order by

    $result = mysql_query("SELECT * FROM Blog ORDER BY Date DESC");
    

    【讨论】:

    • 我可以吻你 GBD :) 赞!
    【解决方案2】:

    如果您的 MySQL 数据库架构使用 DATETIME 作为 Date 列,您可以简单地使用 ORDER BY 在 MySQL 查询中排序:

    $result = mysql_query("SELECT * FROM Blog ORDER BY Date DESC");
    

    【讨论】:

      【解决方案3】:

      如果您有像post_id 这样的自动递增列,那么您也可以使用ORDER BY post_id DESC。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 2010-10-27
        • 1970-01-01
        • 2011-01-13
        相关资源
        最近更新 更多