1 回答
TA贡献1772条经验 获得超5个赞
添加一个last id为$lastid的变量,并在post加载时将其覆盖,并将$lastid设置为last post的数据库id,然后选择一个id大于$lastid的post。这种方法也快速高效。
<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:
<?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 关注
- 147 浏览
添加回答
举报