1 回答

TA貢獻1725條經驗 獲得超8個贊
添加一個$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 關注
- 120 瀏覽
添加回答
舉報