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

时间:2014-07-20 00:20:38

标签: php jquery ajax json

所以我的大部分系统都工作了,而我最后一点就停留了

GET目前使用的是与while循环相同的notification_id,因此它会不断搜索该ID,以便反复搜索新记录。

喜欢......

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


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

我要做的是从while循环中搜索第一个notification_id,将其发送到服务器端viewajax.php以查看是否有新记录以及是否将其插入到其div中(其中目前确实如此)但是然后在下一个ajax循环中使用来自服务器的相同的新notification_id来获取新记录。我已经尝试了所有的东西,并提出了许多问题,我一直在寻找高低在线的可能解决方案无济于事。 任何帮助将不胜感激,我将永远爱你。

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

}

修改

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 个答案:

答案 0 :(得分:0)

为什么不在php部分中检索“notification_status”后将其更新为0?然后它将不再被检索。