doutangshuan6473 2014-07-20 00:20
浏览 96

Ajax通知系统 - 使用最新的notification_id

So I have the majority of my system working and I am stuck on one last bit

The GET is currentlyusing the same notification_id it gets from the while loop, so it constantly searches against that id for new records over and over.

LIKE SO...

jquery....4862996 (line 4)
GET http://viewajax.php?notification_id=43&_=1405814864693


jquery....4862996 (line 4)
GET http://viewajax.php?notification_id=43&_=1405814864694

What I'm looking to do is search the first notification_id from the while loop send it off to the server side viewajax.php to see if there is a new record and if there is insert it into its div, (which it currently does) but then use that same new notification_id from the server on its next ajax cycle for new records. I've tried everything and asked many questions and I've been looking high and low online for possible solutions to no avail. Any help would be greatly appreciated and I will love you forever.

AJAX

<?  
$user1_id=$_SESSION['id'];
$call="select * from notifications WHERE notification_targetuser='$user1_id' AND notification_status=1 ORDER BY notification_id DESC LIMIT 1";
        $chant=mysqli_query($mysqli,$call) or die(mysqli_error($mysqli));

while($notification=mysqli_fetch_array($chant)){
            ?>
             <script type="text/javascript">

function loadIt() {

  var notification_id=<?php echo $notification['notification_id'] ;?>

$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,   
dataType:"json",
cache: false,
success: function(data){
 $("#notif_actual_text-"+notification_id).prepend('<div class="notif_ui"><div class="notif_text"><div  id="notif_actual_text-'+data['notification_id']+'" class="notif_actual_text"><img border=\"1\" src=\"userimages/cropped'+data['notification_triggeredby']+'.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ><br /><a href="'+data['notification_id']+'">'+data['notification_content']+' </a><br />'+data['notification_time']+'<br /></div></div></div></div>');
 i = parseInt($("#mes").text()); $("#mes").text((i+data.num)); 
}
});
}
setInterval(loadIt, 10000);                        
          </script>


<? }}?>

VIEWAJAX.php

if(isset($_GET['notification_id'])){

$id= mysqli_real_escape_string($mysqli,$_GET['notification_id']);
$user1_id= $_SESSION['id'];
$json = array();
$com=mysqli_query($mysqli,"select notification_id,notification_content,notification_time,notification_triggeredby from notifications where notification_id > '$id' AND notification_status='1' ");
echo mysqli_error($mysqli);

$num = mysqli_num_rows($com);
if($num>0){

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

echo json_encode($json);

}

EDIT

PHP SOURCE

{"num":0,"notification_id":null,"notification_content":null,"notification_triggeredby":null,"notification_time":null}

AJAX SOURCE

  <script type="text/javascript">

function loadIt() {


  var notification_id=44
$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,   
dataType:"json",
cache: false,
success: function(data){
 $("#notif_actual_text-"+notification_id).prepend('<div class="notif_ui"><div class="notif_text"><div  id="notif_actual_text-'+data['notification_id']+'" class="notif_actual_text"><img border=\"1\" src=\"userimages/cropped'+data['notification_triggeredby']+'.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ><br /><a href="'+data['notification_id']+'">'+data['notification_content']+' </a><br />'+data['notification_time']+'<br /></div></div></div></div>');
 i = parseInt($("#mes").text()); $("#mes").text((i+data.num)); 
}
});
}
setInterval(loadIt, 10000);              



          </script>
  • 写回答

1条回答 默认 最新

  • dphdh395195 2014-10-07 23:50
    关注

    Why don't you just update "notification_status" to 0 after you retrieve it in the php parts? Then it will not be retrieved again.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号