doucheng9304 2015-11-08 04:05
浏览 44
已采纳

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

Currently when asking the server for data, when one single set is sent back like so

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

the div is inserted.

But lets say there's two sets with different notification id's like so

{"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"}

Nothing happens

So I'll cut the code down as to show and example of what I have

        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)); 
            }

I was toying with the idea of maybe using

  $.each(response, function (i, val) 

But I'm still unsure.

EDIT Exact response how it shows

{"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"}

AND MY WHILE LOOP

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条回答 默认 最新

  • doufanglian7585 2015-11-08 04:32
    关注

    First you need to build an array of notifications, rather than a single one:

    <?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);
    ?>
    

    Then you can access the notifications array from JavaScript:

            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));
                    }
                })
            }
    

    Note, completely untested, but hopefully you can see the difference!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现