0

我写了一个聊天系统,但消息显示在顶部。我希望它们显示在底部。

PHP代码:

<?php
include_once('db.php');

    $selectChats = $db->prepare("SELECT * FROM `chats` ORDER BY `cid` DESC LIMIT 10");

        $selectChats->execute();

            while($ChatsRow = $selectChats->fetch()) {

                echo '<p class="field">';
                echo $ChatsRow['time'];
                echo ' ';
                echo '(';
                echo $ChatsRow['username'];
                echo ')';
                echo ': ';
                echo $ChatsRow['message'];
                echo '</p>';

            }

?>

它的外观图像:

http://puu.sh/3UPpe.png

我希望它从下到上,我该怎么做?

4

1 回答 1

2

只需更改查询的排序顺序将有助于您的情况

SELECT * FROM `chats` ORDER BY `cid` 

由于您需要底部的最新聊天记录,因此您可以执行

SELECT * 
FROM (SELECT * FROM `chats` ORDER BY `cid` DESC LIMIT 10) AS t
ORDER BY `cid`
于 2013-08-06T02:29:59.030 回答