多个数据集在一次调用时通过唯一ID AJAX插入到单独的div中

时间:2015-11-08 04:05:39

标签: javascript php json ajax

目前在向服务器询问数据时,会像这样发送一个单一的

{"num":1,"notification_id":"818","notification_content":
"Lucy  Botham posted a status on your wall","notification_throughurl"}

插入了div。

但是我们可以说有两套不同的通知ID就像这样

{"num":1,"notification_id":"818","notification_content":
"Lucy  Botham posted a status on your wall","notification_throughurl"}

{"num":1,"notification_id":"819","notification_content":
"Lucy  Botham posted a status on your wall","notification_throughurl"}

什么都没发生

因此,我会将代码缩减至显示和我拥有的示例

        success: function(response){
            if(response.notification_id > notification_id){

            $("#notif_ui"+ notification_id).prepend('
    <div class="notif_text"><div  id="notif_actual_text-'+response['notification_id']+'" 
        class="notif_actual_text"><img border=\"1\" src=\"userimages/cropped'+response
            ['notification_triggeredby']+'.jpg\" 
onerror=this.src=\"userimages/no_profile_img.jpeg\" 
        width=\"40\" height=\"40\" ><br /></div></div>');
                  i = parseInt($("#mes").text()); $("#mes").text((i+response.num)); 
            }

我正在玩弄可能使用

的想法
  $.each(response, function (i, val) 

但我仍然不确定。

EDIT 确切的回应如何显示

{"num":1,"notification_id":"823","notification_content":"Lucy  Botham posted a status on your wall","notification_throughurl"
:"singlepoststreamitem.php?streamitem_id=703","notification_triggeredby":"85","notification_status":"1"
,"notification_time":"2015-11-08 04:16:26"}{"num":1,"notification_id":"824","notification_content":"Lucy
  Botham posted a status on your wall","notification_throughurl":"singlepoststreamitem.php?streamitem_id
=704","notification_triggeredby":"85","notification_status":"1","notification_time":"2015-11-08 04:16
:27"}

和我的圈圈

while($row = mysqli_fetch_assoc($com)){
if($row['notification_status']==1){
$num = mysqli_num_rows($com);

if($num){
    $json['num'] = 1;
}else{
    $json['num'] = 0;
}
    $json['notification_id'] = $row['notification_id'];
    $json['notification_content'] = $row['notification_content'];
    $json['notification_throughurl'] = $row['notification_throughurl'];
    $json['notification_triggeredby'] = $row['notification_triggeredby'];
    $json['notification_status'] = $row['notification_status'];
    $json['notification_time'] = $row['notification_time'];



echo json_encode($json);
}}

2 个答案:

答案 0 :(得分:1)

您的php可以更改为

// you can just the the number of rows once outside the while loop
$num = mysqli_num_rows($com);
if($num){
    $jsonNum = 1;
}else{
    $jsonNum = 0;
}
while($row = mysqli_fetch_assoc($com)){
    if($row['notification_status']==1){ // this would be unnecessary if you add it as a where conditional to your sql query
        // add the num to the array to match your current data structure
        $row['num'] = $jsonNum;
        $json[] = $row;
    }
}
echo json_encode($json);

答案 1 :(得分:1)

首先,您需要构建一组通知,而不是单个通知:

<?php
$json = array(
    'notifications' => array()
);

while ($row = mysqli_fetch_assoc($com)) {
    if ($row['notification_status'] == 1) {
        $num = mysqli_num_rows($com);

        $notification = array();
        if ($num) {
            $notification['num'] = 1;
        } else {
            $notification['num'] = 0;
        }
        $notification['notification_id'] = $row['notification_id'];
        $notification['notification_content'] = $row['notification_content'];
        $notification['notification_throughurl'] = $row['notification_throughurl'];
        $notification['notification_triggeredby'] = $row['notification_triggeredby'];
        $notification['notification_status'] = $row['notification_status'];
        $notification['notification_time'] = $row['notification_time'];
        $json['notifications'][] = $notification;
    }
}

echo json_encode($json);
?>

然后您可以从JavaScript访问notifications数组:

        success: function(response) {

            $.each(response.notifications, function(i, notification) {

                if (notification.notification_id > notification_id) {

                    $("#notif_ui" + notification_id).prepend('<div class="notif_text"><div id="notif_actual_text-' + notification['notification_id'] + '" class="notif_actual_text"><img border=\"1\" src=\"userimages/cropped' + notification['notification_triggeredby'] + '.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ><br /></div></div>');
                    i = parseInt($("#mes").text());
                    $("#mes").text((i + response.num));
                }
            })
        }

注意,完全未经测试,但希望你能看到差异!