我写了一个聊天系统,但消息显示在顶部。我希望它们显示在底部。
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>';
}
?>
它的外观图像:
我希望它从下到上,我该怎么做?
答案 0 :(得分:2)
只需更改查询的排序顺序就会有所帮助
SELECT * FROM `chats` ORDER BY `cid`
因为您需要最近的聊天记录,所以
SELECT *
FROM (SELECT * FROM `chats` ORDER BY `cid` DESC LIMIT 10) AS t
ORDER BY `cid`