1 回答
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
添加一個(gè)last id為$lastid的變量,并在post加載時(shí)將其覆蓋,并將$lastid設(shè)置為last post的數(shù)據(jù)庫id,然后選擇一個(gè)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 關(guān)注
- 188 瀏覽
添加回答
舉報(bào)
