0

我正在开发一个评论系统,我正在尝试构建一个函数,使用 ajax 从数据库中查询和显示 10 条最新评论。

我遇到的问题是,我只是不知道如何使用ajaxjson_encode显示行。

当我使用 php 显示多行时,我使用.=,这非常简单,但是使用 ajax 和 json_encode,我目前只能显示一个结果。

下面是我的代码的纯 PHP 版本,用于显示多行

public function display_comment(){
    $query = "SELECT * FROM 'comment' WHERE 'user_id' = $user_id ORDER BY 'save_time' DESC LIMIT 10";
    $result = $database->query($query);

    foreach($result as $row){
        $user_id = $row['user_id'];
        $user_name = $row['user_name'];
        $comment = $row['comment'];

        $all_comments .= '<div id="' . $user_id . '"><span>' . $user_name . '</span><span>' . $comment . '</span></div>';
    }
}

使用 ajax 和 json_encode,这就是我返回单个结果的方式

public function display_comment(){
    $query = "SELECT * FROM 'comment' WHERE 'user_id' = $user_id ORDER BY 'save_time' DESC LIMIT 10";
    $result = $database->query($query);

    foreach($result as $row){
        $user_id = $row['user_id'];
        $user_name = $row['user_name'];
        $comment = $row['comment'];

        $json_array = array("user_id" => $user_id, "user_name" => $user_name, "comment" => $comment);
        header('Content-type:application/json');
        exit(json_encode($json_array)); // I am using exit instead of echo, because somehow if I use echo, it returns entire html page. 
    }
}

jQuery部分

$(document).on("click","#view_comment",function(e){
    e.preventDefault();
    $("#view_comment").text("loading..");
    var view_comment = $(this).val(); // Don't worry about this

    $.ajax({
        type:"post",
        data:{"view_comment":view_comment},
        dataType:"json",
        success:function(data){
            $('#ajax_comment').html('<div id="' + data.user_id + '"><span>' + data.user_name + '</span><span>' + data.comment + '</span></div>');
        },
        error:function(data){
            // Error code
        }
    });
});

我应该如何更改我的代码以使用 json_encode 显示多行?

非常感谢您!

4

1 回答 1

4

您只能查看单行的原因是因为在while()循环的第一次迭代之后您已经存在脚本:

exit(json_encode($json_array));

将php更改为:

public function display_comment(){
  $query = "SELECT * FROM 'comment' WHERE 'user_id' = $user_id ORDER BY 'save_time' DESC LIMIT 10";
  $result = $database->query($query);
  $results = array();
  foreach($result as $row){
    $user_id = $row['user_id'];
    $user_name = $row['user_name'];
    $comment = $row['comment'];

    $results[] = array("user_id" => $user_id, "user_name" => $user_name, "comment" => $comment);

  }
  header('Content-type:application/json');
  exit(json_encode($results)); 
}

Javascript

$(document).on("click","#view_comment",function(e){
    e.preventDefault();
    $("#view_comment").text("loading..");
    var view_comment = $(this).val(); // Don't worry about this

    $.ajax({
        type:"post",
        data:{"view_comment":view_comment},
        dataType:"json",
        success:function(data){
            var html = '', comment;
            for(var i = 0; i < data.length; i++) {
                comment = data[i];
                html += '<div id="' + comment.user_id + '"><span>' + comment.user_name + '</span><span>' + comment.comment + '</span></div>'
            }
            $('#ajax_comment').html(html);
        },
        error:function(data){
            // Error code
        }
    });
});
于 2012-08-02T07:24:43.460 回答