AJAX GET - 最新的记录

时间:2014-07-19 14:43:06

标签: php ajax json mysqli polling

所以我尝试了过去5天获取新数据的各种方法,并且我决定让这个通知系统完成,因为我不喜欢在完成新事物之前没有完成任务。我尝试了本地存储,但没有达到预期的效果

所以我首先创建了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_time="<?php echo $notification['notification_time'] ;?>"


$.ajax({
type: "GET",
url: "viewajax.php?notification_time="+notification_time,   
dataType:"json",
cache: false,
success: function(response){
    if (response.notificiation_time > notification_time) {
      $("#notif_actual_text-"+notification_time).prepend('<div class="notif_ui"><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 /><a href="'+response['notification_id']+'">'+response['notification_content']+' </a><br />'+response['notification_time']+'<br /></div></div></div></div>');

      i = parseInt($("#mes").text()); $("#mes").text((i+response.num)); 
    }
}
});
}
setInterval(loadIt, 10000);


</script>

<? } }?>

但是这就是我陷入困境的地方,因为我想使用那个notification_time来搜索服务器以寻找旧版本的新东西..将其传回并放入指定的div中,然后在下一个使用新的notification_time搜索而不是我刚刚用来获得新的那个,因为这对于我想要实现的东西来说是毫无意义的。我们都知道社交网络是如何做到的,问题是,我将是第一个承认,它让我的生活混乱......

这是我的Php,显然不完整。

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

$id= mysqli_real_escape_string($mysqli,$_GET['notification_time']);
$user1_id= mysqli_real_escape_string($mysqli,$_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_targetuser='$user1_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'];
$json['notification_triggeredby'] = $resultArr['notification_triggeredby'];
$json['notification_time'] = $resultArr['notification_time'];
mysqli_free_result($com);

echo json_encode($json);

}

3 个答案:

答案 0 :(得分:1)

粗心的全局变量方法将是这样的:

var notification_time="<?php echo $notification['notification_time'] ;?>"; // make global

function loadIt() {



$.ajax({
type: "GET",
url: "viewajax.php?notification_time="+notification_time,   
dataType:"json",
cache: false,
success: function(response){

    if (response.notificiation_time > notification_time) {
      $("#notif_actual_text-"+notification_time).prepend('<div class="notif_ui"><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 /><a href="'+response['notification_id']+'">'+response['notification_content']+' </a><br />'+response['notification_time']+'<br /></div></div></div></div>');

      i = parseInt($("#mes").text()); $("#mes").text((i+response.num)); 
    }
    notification_time = response.notificiation_time; // update global after each call
}
});
}
setInterval(loadIt, 10000);

答案 1 :(得分:0)

快速获取此功能的方法是,如果您存储了&#39; notification_time&#39;变量中的最后一个查询,并根据新的&#39; notification_time&#39;

检查该变量
if(!isset($time)){
 $time = $notification['notification_time'];
}else{
 if($time < $notification['notification_time']){
  //update the div
  $time = $notification['notification_time'];
 }
}

答案 2 :(得分:0)

您的notification_time变量每次都需要通过AJAX查询中的success回调进行更新。

所以它需要超出你的loadIt功能。您可以查看jQuery data method。这将允许您将其附加到通知列表的父元素之类的某个位置,并避免不小心解决全局问题。

其他一些建议:

  • 在这里看起来像一个错字:if (response.notificiation_time > notification_time) {
  • 避免在AJAX回调中使用字符串连接来生成新的DOM元素。请改用jQuery元素$newEl = $('<div />')...;
  • 重写网页顶部的SQL代码并删除while循环。那个循环给人一种完全错误的印象。