多个Json字符串?

I'm trying to get multiple strings of data and put them into there designated divs.

I'm building a view more comments function, so when someone clicks on view all it replaces the 2 compulsory shown comments and if there is more then replace the original two and add any more that may be on that post.

Its not working and I know for a fact its the server side php script that is the issue as Ive checked all the ajax with error reporting. I use the while loop to grab all the data against comment_streamitem and in my database there is four results. it gets these four results(Seen in firebug) but because the json_encode is in the loop i'm pretty sure this is why its not going into its div. But when I put it outside the div it only grabs 1 result.

How would I get around this?

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' order by comment_id";
$check1 = mysqli_query($mysqli,$check) or die(mysqli_error($mysqli));
    $json = array();
while($resultArr = mysqli_fetch_array($check1)){


$json[$resultArr['comment_id']]['comment_id'] = $resultArr['comment_id'];
$json[$resultArr['comment_poster']]['comment_poster'] = $resultArr['comment_poster'];
$json[$resultArr['comment_streamitem']]['comment_streamitem'] = $resultArr['comment_streamitem'];
$json[$resultArr['comment_datetime']]['comment_datetime'] = $resultArr['comment_datetime'];
$json[$resultArr['comment_content']]['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[$resultArr['username']]['username'] = $resultArr['username'];
$json[$resultArr['id']]['id'] = $resultArr['id'];
$json[$resultArr['first']]['first'] = $resultArr['first'];
$json[$resultArr['middle']]['middle'] = $resultArr['middle'];
$json[$resultArr['last']]['last'] = $resultArr['last'];


}
}
echo json_encode($json);

Ok with the above I now get the below response in firebug but its posting 1 comment into the div and everything is undefined. I've included the ajax call too

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

The Ajax script:

<script type="text/javascript">
$(function()
{
$(".view_comments").click(function()
{
var ID = $(this).attr("id");

$.ajax({
type: "GET",
url: 'viewmorecommentslink.php?comment_streamitem='+ ID,
dataType: 'json',
success: function(response){
$("#comment_list_"+ID).html('<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></div>');
$("#view"+ID).remove();

}   
});
return false
});
});
</script>

Just this simple loop should give you the array of objects

As for the user data that you are trying to merge, it is better if you can create a JOIN query.

Running SELECT query in a loop could be expensive.


$sql="
    SELECT streamdata_comments.comment_id
          ,streamdata_comments.comment_poster
          ,streamdata_comments.comment_streamitem
          ,streamdata_comments.comment_datetime
          ,streamdata_comments.comment_content 
          ,user.username
          ,user.id
          ,user.first
          ,user.middle
          ,user.last
    FROM streamdata_comments 
    INNER JOIN user
    ON streamdata_comments.comment_poster=user.id
    WHERE streamdata_comments.comment_streamitem = $id
    ORDER BY streamdata_comments.comment_id
"
;



$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
$json = array();
while($row = mysqli_fetch_array($result)){
    array_push($json, $row);
}

You are overwriting your results in the loop:

$json['comment_id'] = $resultArr['comment_id'];
// etc.

Should be something like:

$json[$resultArr['comment_id']]['comment_id'] = $resultArr['comment_id'];
      ^^^^^^^^^^^^^^^^^^^^^^^^ or you use a counter or something similar
// etc.

You should also use a prepared statement or escape your variables before you use them in an sql query.