我希望你一切都好。嗯,我正在聊天室工作,我面临的问题是,当页面加载时它显示第一条消息,我正在考虑如何使其在页面加载时显示最后/新消息... 。 这是显示数据的代码:
</div>
<!-- Sidebar -->
<div class="w3-sidebar w3-light-grey w3-bar-block" style="width:83.6%;height:75.4%">
<h3 class="w3-bar-item">Menu</h3>
<div id="display"></div>
</div>
<div class="w3-sidebar w3-light-grey w3-bar-block" style="width:16.5%;height:75.4%;margin-left:1140px;">
<h3 class="w3-bar-item">Menu</h3>
<div>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
</div>
</div>
<script>
setInterval(function(){
$('#display').load("pages/get.php").fadeIn("slow");
}, 1000);
</script>
get.php:
<?php
require('../system/db.php');
global $con;
$get = mysqli_query($con,"SELECT * FROM zadx_chat ORDER BY id");
if(mysqli_num_rows($get) > 0){
while($row = mysqli_fetch_array($get)){
echo "<p>" . $row['message'] . "</p>";
}
}
?>
答案 0 :(得分:0)
检查您的page.php,如果有查询以获取消息,请确保将查询更改为降序。就像是否存在ORDER BY table_column ASC,然后通过table_column DESC将其更改为ORDER。那应该为您工作。
答案 1 :(得分:0)
将查询更改为
$get = mysqli_query($con,"SELECT * FROM zadx_chat ORDER BY created_at desc");
您需要确保created_at
是日期时间字段。您也可以将order by id desc
用于简单的数据库。
答案 2 :(得分:-1)
您可以使用jquery的 insertAfter 函数在html页面中设置数据,并为包含标签列表的div设置ID
<div id='starting_div'>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
<h1>asdasd</h1>
</div>
$(your_dynamic_data).insertAfter('#starting_div');