douzheng0702 2013-08-05 20:22
浏览 35
已采纳

正确列出网站文章

So I've been using PHP to list my website's articles via MySQL. The problem is, the posts are supposed to be listed with newest at the top, but instead oldest posts are at the top. How can I change this. Here is a snippet of source code:

<?php foreach ($posts as $post): ?>
   <div class="container">
      <h2 class="title"><a href=""><?php echo htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8'); ?></a></h2>
      <p class="date"><?php echo htmlspecialchars($post['date'], ENT_QUOTES, 'UTF-8'); ?></p>
      <div class="body">
      <p><?php echo htmlspecialchars($post['content'], ENT_QUOTES, 'UTF-8'); ?></p>
      </div>
   </div>
   <?php endforeach; ?>

try {

   $sql = 'SELECT id, title, date, content FROM post';
   $result = $pdo->query($sql);

} catch (PDOException $e) {

   $error = 'Error connecting to database server: ' . $e->getMessage();
   exit();

}
  • 写回答

1条回答 默认 最新

  • dstnlhhv791576 2013-08-05 20:26
    关注

    In your initial query, be sure to include the line

    ORDER BY $column 
    

    and then either

    ASC
    

    or

    DESC
    

    Where $column references the date column, I would presume.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!