1 回答
TA贡献1725条经验 获得超7个赞
添加一个$lastid的上一个 id 变量,并在加载帖子时覆盖它,并将$lastid设置为最后一个帖子的数据库 ID,然后选择一个 id 大于该$lastid的帖子。这种方法也是快速有效的。
<script>
var lastid = 1;
function fetchpost() {
$.ajax({
url:"getPosts.php?lastid="+lastid, //the page containing getting post code
type: "post",
dataType: 'json',
data: '',
success:function(result){
$("#posts_parent").after(result);
//Getting id of last post using :last
lastid = $("posts_parent:last").attr("id");
}
});
}
<script>
<div id="posts_parent">
</div>
<button onlclick="fetchpost()">Fetch New Posts</button> //Button fetches new posts
菲律宾比索:
<?php
$lastidofpost = $GET['lastid'];
$sql=mysql_query("SELECT * FROM posts ORDER BY id DESC WHERE id > $lastidofpost LIMIT 10"); // Limit 10, only 10 posts will be fetched
while($row=mysql_fetch_array($sql))
{
$id= $row['id'];
$postDiscription = $row['disc'];
?>
<div id="<?php echo $id; ?>">
<?php echo $postDiscription; ?>
</div>
<?php
}
?>
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报