php - 如何将评论分成页面

标签 php jquery mysql ajax comments

我使用 jQuery Ajax 函数从 MySQL 数据库获取评论,并且收到 JSON 数组。

评论加载。

<?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

关于php - 如何将评论分成页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996576/

相关文章:

javascript - 允许 ASP.NET Framework 上的特定按钮上的 Javascript Onbeforeunload 函数

C# 如何: listening if new record is inserted in a mysql table

php - 在 PHP 中向已实例化的对象添加自定义函数?

php - ZF2 服务管理器

php - $_SERVER ['DOCUMENT_ROOT' 出现问题]

javascript - 将 ArrayList 传递给 JQuery Flot

php - AJAX 成功函数未执行

javascript - 如何自动水平滚动到div内的中间?

mysql - 使用 mysql 和 nodeJS 构建一个 docker

python - 使用 Anaconda 安装 MySQLdb