将4个数据字符串放入div中

时间:2014-08-27 21:25:39

标签: php ajax json

好的,我最后一次尝试这个,因为它绞尽脑汁。

当我在服务器端外部添加json_encode时,它会返回1个数据串并将其插入到div中。但是只要我把它放在while循环中并且如果它收集超过1(目前在测试阶段中有4个,我希望它做),它就不会将这些4放入其单独的div中。有人可以向我解释为什么会这样。

这是我的ajax

$(function() {
  $(".view_comments").click(function() {
    var commentID = $(this).attr("id");

    $.ajax({
      type: "GET",
      url: "viewmorecommentslink.php?comment_streamitem=" + commentID,
      dataType: "json",
      success: function(response) {
        $("#view" + commentID).remove();
        $("#comment_list_" + commentID).append('<div class="stream_comment" id="comment_' + response['comment_id'] + '" style="margin-top:0px;">\
<table width=100%><tr><td valign=top width=30px><img class="stream_profileimage" style="border:none;padding:0px;display:inline;" border=\"0\" src=\"userimages/cropped' + response['comment_poster'] + '.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ></a><td valign=top align=left>\
<a href="/profile.php?username=' + response['username'] + '">' + response['first'] + ' ' + response['middle'] + ' ' + response['last'] + '</a> - <abbr class="timeago" title=' + response['comment_datetime'] + '>' + response['comment_datetime'] + '</abbr>\<div class="commentholder">' + response['comment_content'] + '</div><br/>\<div id="commentactivitycontainer">\
<a style="cursor:pointer;" onClick=\"deletecomment(' + response['comment_id'] + ',comment_' + response['comment_id'] + ');\">Delete</a><a id="likecontext_' + response['comment_id'] + '" style="cursor:pointer;" onClick=\"likestatuscomment(' + response['comment_id'] + ',this.id);\">\
<div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint' + response['comment_id'] + '">Like</div></a><div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint' + response['comment_id'] + '"></div>\
</form><a id="dislikecontext_' + response['comment_id'] + '" style="cursor:pointer;" onClick=\"dislikestatuscomment(' + response['comment_id'] + ',this.id);\"><div style="width:90px; position:relative;top:-0px; float:left; left:200px" id="dislikescommentprint' + response['comment_id'] + '">Dislike</div>\
</a><div style="width:90px; position:relative; top:-0px; float:left; left:200px" id="dislikescommentprint' + response['comment_id'] + '"></div></form></div></table></div>');

      }
    });
    return false
  });
});

PHP

if (isset($_GET['comment_streamitem'])) {
  $id = $id = mysqli_real_escape_string($mysqli, $_GET['comment_streamitem']);

  $check = "select comment_id,comment_poster,comment_streamitem,comment_datetime,comment_content FROM streamdata_comments WHERE comment_streamitem='$id'";
  $check1 = mysqli_query($mysqli, $check) or die(mysqli_error($mysqli));
  $json = array();
  while ($resultArr = mysqli_fetch_array($check1)) {
    $json['comment_id'] = $resultArr['comment_id'];
    $json['comment_poster'] = $resultArr['comment_poster'];
    $json['comment_streamitem'] = $resultArr['comment_streamitem'];
    $json['comment_datetime'] = $resultArr['comment_datetime'];
    $json['comment_content'] = $resultArr['comment_content'];

    $user = $resultArr['comment_poster'];
    $check2 = "SELECT * FROM user WHERE id='$user'";
    $check22 = mysqli_query($mysqli, $check2);
    $resultArr = mysqli_fetch_array($check22);
    $json['username'] = $resultArr['username'];
    $json['id'] = $resultArr['id'];
    $json['first'] = $resultArr['first'];
    $json['middle'] = $resultArr['middle'];
    $json['last'] = $resultArr['last'];
    echo json_encode($json);
  }
}

的字符串

{
  "comment_id": "1690",
  "comment_poster": "33",
  "comment_streamitem": "223",
  "comment_datetime": "2014-08-23 17:24:17",
  "comment_content": "kk",
  "username": "luce",
  "id": "33",
  "first": "lucy",
  "middle": "",
  "last": "ward"
} {
  "comment_id": "1689",
  "comment_poster": "33",
  "comment_streamitem": "223",
  "comment_datetime": "2014-08-23 17:24:15",
  "comment_content": "kkk",
  "username": "luce",
  "id": "33",
  "first": "lucy",
  "middle": "",
  "last": "ward"
} {
  "comment_id": "1688",
  "comment_poster": "33",
  "comment_streamitem": "223",
  "comment_datetime": "2014-08-23 17:24:13",
  "comment_content": "hh",
  "username": "luce",
  "id": "33",
  "first": "lucy",
  "middle": "",
  "last": "ward"
} {
  "comment_id": "1687",
  "comment_poster": "33",
  "comment_streamitem": "223",
  "comment_datetime": "2014-08-23 17:24:10",
  "comment_content": "ggg",
  "username": "luce",
  "id": "33",
  "first": "lucy",
  "middle": "",
  "last": "ward"
}

1 个答案:

答案 0 :(得分:1)

您需要返回一个有效的JSON数组:

  $result = array();
  while ($resultArr = mysqli_fetch_array($check1)) {
    $json = array();
    $json['comment_id'] = $resultArr['comment_id'];
    $json['comment_poster'] = $resultArr['comment_poster'];
    $json['comment_streamitem'] = $resultArr['comment_streamitem'];
    $json['comment_datetime'] = $resultArr['comment_datetime'];
    $json['comment_content'] = $resultArr['comment_content'];

    $user = $resultArr['comment_poster'];
    $check2 = "SELECT * FROM user WHERE id='$user'";
    $check22 = mysqli_query($mysqli, $check2);
    $resultArr = mysqli_fetch_array($check22);
    $json['username'] = $resultArr['username'];
    $json['id'] = $resultArr['id'];
    $json['first'] = $resultArr['first'];
    $json['middle'] = $resultArr['middle'];
    $json['last'] = $resultArr['last'];
    array_push($result, $json);
  }
  echo json_encode($result);

请注意,在输出之前,我实例化一个$result数组并将每个对象推入其中。

您还必须更新您的javascript以处理数组:

    $.ajax({
      type: "GET",
      url: "viewmorecommentslink.php?comment_streamitem=" + commentID,
      dataType: "json",
      success: function(responses) {
        $("#view" + commentID).remove();
        for (var i = 0; i < responses.length; i++) {
          var response = responses[i];
          $("#comment_list_" + commentID).append('<div class="stream_comment" id="comment_' + response['comment_id'] + '" style="margin-top:0px;">\
<table width=100%><tr><td valign=top width=30px><img class="stream_profileimage" style="border:none;padding:0px;display:inline;" border=\"0\" src=\"userimages/cropped' + response['comment_poster'] + '.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ></a><td valign=top align=left>\
<a href="/profile.php?username=' + response['username'] + '">' + response['first'] + ' ' + response['middle'] + ' ' + response['last'] + '</a> - <abbr class="timeago" title=' + response['comment_datetime'] + '>' + response['comment_datetime'] + '</abbr>\<div class="commentholder">' + response['comment_content'] + '</div><br/>\<div id="commentactivitycontainer">\
<a style="cursor:pointer;" onClick=\"deletecomment(' + response['comment_id'] + ',comment_' + response['comment_id'] + ');\">Delete</a><a id="likecontext_' + response['comment_id'] + '" style="cursor:pointer;" onClick=\"likestatuscomment(' + response['comment_id'] + ',this.id);\">\
<div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint' + response['comment_id'] + '">Like</div></a><div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint' + response['comment_id'] + '"></div>\
</form><a id="dislikecontext_' + response['comment_id'] + '" style="cursor:pointer;" onClick=\"dislikestatuscomment(' + response['comment_id'] + ',this.id);\"><div style="width:90px; position:relative;top:-0px; float:left; left:200px" id="dislikescommentprint' + response['comment_id'] + '">Dislike</div>\
</a><div style="width:90px; position:relative; top:-0px; float:left; left:200px" id="dislikescommentprint' + response['comment_id'] + '"></div></form></div></table></div>');
        }

      }
    });