dounaidu0204 2014-08-25 16:44
浏览 13

使用Ajax查看所有评论

I'm trying to put together a view more comments function. So my original post with the id of comment_streamitem is the original posts id and the comment_id is the comments id. I show two comments on the posts then an view all link shows.

I am currently able to get all the comments from the database through $_GET JSON array and it shows as such in Firebug. The comment_list_ID removes the two original comments and removes the view link but doesn't insert the 4 retrieved comments from the database the area where they should insert into its div stays blank.

Could someone look over my code for any possible oversights or errors please?

Thank you

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

$.ajax({
type: "GET",
url: "viewmorecommentslink.php?comment_streamitem="+ ID,
success: function(data){
$("#comment_list_"+ID).append('<div id="comment_list_'+data['comment_streamitem']+'"><div class="stream_comment" id="comment_'+data['comment_id']+' style="margin-top:0px; background-color:#000000;"><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'+data['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='+data['username']+'">'+data['first']+' '+ data['middle']+' '+data['last']+'</a> - <abbr class="timeago" title="'+data['comment_datetime']+'">'+data['comment_datetime']+'</abbr><div class="commentholder">'+data['comment_content']+'</div><br/>\<div id="commentactivitycontainer"><a style="cursor:pointer;" onClick=\"deletecomment('+data['comment_id']+',comment_'+data['comment_id']+');\">Delete</a><a id="likecontext_'+data['comment_id']+'" style="cursor:pointer;" onClick=\"likestatuscomment('+data['comment_id']+',this.id);\"><div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint'+data['comment_id']+'">Like</div></a><div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint'+data['comment_id']+'"></div></form>\
<a id="dislikecontext_'+data['comment_id']+'" style="cursor:pointer;" onClick=\"dislikestatuscomment('+data['comment_id']+',this.id);\"><div style="width:90px; position:relative;top:-0px; float:left; left:200px" id="dislikescommentprint'+data['comment_id']+'">Dislike</div></a><div style="width:90px; position:relative; top:-0px; float:left; left:200px" id="dislikescommentprint'+data['comment_id']+'"></div></form></div></div></table></div></div></div></div></table></div></div>');
$("#view"+ID).remove();
$("#comment_list_"+ID).remove();
}
});
return false;
});
});
</script>

Viewcommentslink.php

<?php
require"include/rawfeeds_load.php";

if(isset($_GET['comment_streamitem'])){
$id=$_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));

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);
}}
  • 写回答

1条回答 默认 最新

  • dq1230123 2014-08-25 18:09
    关注

    You are echoing the json in a loop, creating a number of json strings. You should only echo a single json string - to do this add to an array in the loop then output that array after the loop:

    $temp = 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);
    
        $temp[]=$json;
    
    }
    
    echo json_encode($temp);
    die();
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致