weixin_33674976 2014-07-15 22:16 采纳率: 0%
浏览 31

Ajax,使用num_rows更新div

I've built a notification system, and its almost working. I just have one niggly bit that I can't seem to get my head around. When a new update comes in from a friend it prints out the number of new notifications as expected, only if a user posts twice num_rows 2 pops up.. but if a user posts again it updates and replaces the 2 new notifications number back to 1 in the div because I'm using html in the ajax to replace. So my question is, how can I update the div to get the total amount of results so it goes 1,2,3,4 etc instead of 2,1,1,1,1. I don't want to replace the num of new rows with only the (1) update in the div, just add to the amount of new updates already inside it.

A bit like when facebook shows amount of notifications. say I have two and a friends posts on my wall I then will have 3.. but at the moment its adding the last new num row and going back to 1.

AJAX

<script type="text/javascript">
function loadIt() {
  var notification_id="<?php echo $notification_id['notification_id'] ;?>"
  var notification_id= window.localStorage.getItem ('lastId'); 
$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,   
dataType:"json",
cache: false,
 success: function(response){
 if(response){
    dataHandler;
    if(response.num){
       window.localStorage.setItem('lastId', response.notification_id);

  var dataHandler = function(response){
   var isDuplicate = false, storedData = window.localStorage.getItem ('lastId');

   for (var i = 0; i < storedData.length; i++) {
     if(storedData[i].indexOf(response) > -1){
        isDuplicate = true;
     }
   }
   if(!isDuplicate){
     storedData.push(response);
   }
}; 
if(response){
   if(response.num){
      $("#notif_actual_text-"+notification_id).prepend('<div  id="notif_actual_text-'+response['notification_id']+'" class="notif_actual_text"><a href="'+response['notification_id']+'">'+response['notification_content']+' </a><br />'+response['notification_time']+'</div></nr>');

      $("#mes").html(''+ response.num + '');
    }
};     
}
}   
}
});
}
setInterval(loadIt, 10000);

PHP

$json = array();
$com=mysqli_query($mysqli,"select notification_id,notification_content,notification_time from notifications where notification_id > '$id' AND notification_status=1 ");
echo mysqli_error($mysqli);

$num = mysqli_num_rows($com);
if($num!=1){

    $json['num'] = $num;
}else{
    $json['num'] = 0;
}
$resultArr = mysqli_fetch_array($com);
$json['notification_id'] = $resultArr['notification_id'];
$json['notification_content'] = $resultArr['notification_content'];

mysqli_free_result($com);
header('Content-Type: application/json');
echo json_encode($json);

}
  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2014-07-15 22:25
    关注

    The number of notifications on client-side is storedData.length.

    So i would replace the counter

    $("#mes").html(''+ response.num + '');
    

    with

    $("#mes").html(storedData.length);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题