我从我的MySQL数据库与jQuery Ajax函数的评论,我收到JSON数组。
commentsLoad .
<?php
include('config.php');
$newsid = $_GET['newsid'];
$comments=array();
$commentsQuery = "SELECT * FROM comments
where fk_news like ".$newsid;
$result = $conn->query($commentsQuery);
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
$comments[]=array('id' => $row['id'], 'name' => $row['cnick'], 'text' => $row['ctext'], 'date' => $row['cdate']);
}
}
echo json_encode($comments);
exit;
?>
我的Javascript:
$('.toggleComments').click(function(){
var commentsPosition = $(this).closest('div').next().find('.userComments');
var newsid = $(this).data('newsid');
if(!$(this).hasClass('commentsReady')){
$(this).addClass('commentsReady');
console.log("Getting comments...");
$.ajax({
type: 'GET',
url: commentsUrl,
dataType: "json",
data:{newsid:newsid},
success: function(comments){
$.each(comments, function(i, komentar){
addComment(commentsPosition,komentar);
})
},
error: function(e){
console.log(e);
}
});
}
});
我想把每一页的注释分成5条
如果你想限制你的评论每页5条,使用限制原因,如:
$commentsQuery = "SELECT * FROM comments where fk_news like ".$newsid limit 5;
如果您想限制并获得最新的5,您可以(除了上面的)添加订单:
$commentsQuery = "SELECT * FROM comments where fk_news like ".$newsid limit 5 order by cdate desc;
如果你想拉出所有的评论,但显示分页,让用户只看到5条评论,然后点击下一个(或页码),然后跳转到下一个,你可以使用任何JS或JQuery分页插件,如:SimplePagination.js