从下到上为博客发布系统吐出数据库字段

时间:2013-10-22 04:39:29

标签: php content-management-system blogs output

我有一个PHP代码,其中还包括分页

<?php
//to make pagination
$statement = "`blog_posts`";
        //show users
        $query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");

        while ($row = mysql_fetch_assoc($query)) {
        echo '
            <div class="box">
            <a class="title" href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a>
            <hr/>
            <p>'.$row['postDesc'].'</p>
            <div class="clear"></div>
            <hr/>
            <div class="desc">
            Posted on '.date("jS M Y H:i:s", strtotime($row["postDate"])).'
            <div class="underside"><a href="viewpost.php?id='.$row['postID'].'">Read More</a></div>
            </div>
            </div>              
            ';
        }

echo pagination($statement,$limit,$page);

如果我有三个这样的帖子......

ID      TITLE
-------------
1       Apples
2       Post 2
3       Grapes

我的博客会像这样吐出来......

Apples
Post 2
Grapes

这很糟糕,因为最新的帖子位于页面的最底部(或最后一页)

我想要它,以便最新的文章首先出现。

我该怎么做?请帮助解释我当前代码无法正常工作的原因。

3 个答案:

答案 0 :(得分:0)

SELECT * FROM {$statement} ORDER BY `date` DESC LIMIT {$startpoint} , {$limit} 

日期应该是日期,博客已保存

答案 1 :(得分:0)

如下所示更改您的查询并按

添加订单
$query = mysql_query("SELECT * FROM {$statement} ORDER BY id desc LIMIT {$startpoint} , {$limit}");

答案 2 :(得分:0)

Please, use order by column for sorting as per desc or asc order :

 <?php
    //to make pagination
    $statement = "`blog_posts`";
            //show users
            $query = mysql_query("SELECT * FROM {$statement} ORDER BY date desc LIMIT {$startpoint} , {$limit}");

            while ($row = mysql_fetch_assoc($query)) {
            echo '
                <div class="box">
                <a class="title" href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a>
                <hr/>
                <p>'.$row['postDesc'].'</p>
                <div class="clear"></div>
                <hr/>
                <div class="desc">
                Posted on '.date("jS M Y H:i:s", strtotime($row["postDate"])).'
                <div class="underside"><a href="viewpost.php?id='.$row['postID'].'">Read More</a></div>
                </div>
                </div>              
                ';
            }

    echo pagination($statement,$limit,$page);