0

我有一个 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

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

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

我该怎么做。请帮助解释为什么我当前的代码不能正常工作。

4

3 回答 3

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

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

于 2013-10-22T04:41:19.823 回答
0

如下更改您的查询并添加订单

$query = mysql_query("SELECT * FROM {$statement} ORDER BY id desc LIMIT {$startpoint} , {$limit}");
于 2013-10-22T04:41:32.827 回答
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);
于 2013-10-22T04:46:27.557 回答