Search code examples
phpajaxjsonmysqlipolling

AJAX GET - Newest records by time


So I've tried various ways of getting new data over the past 5 days and I'm determined to get this notifications system completed as I don't like not finishing something before moving on to something new. I tried local storage but it didn't have the desired affect

So I firstly created the ajax to send the last notification by time to the server side from client side and get any new one for the session user like so.

<?  
$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>

<? } }?>

But this is where I become stuck, because I want to use that notification_time to search the server for anything newer against the old one.. pass it back and put it in its designated div and then use that new notification_time on the next search instead of the same one I just used to get the new one, as that would be pretty pointless for what I'm wanting to achieve. We all know how social networks do it, problem is, I'll be the first one to admit, its confusing the life out of me...

Here is my Php which is obviously not complete.

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

}

Solution

  • The careless global variable method would go something like this:

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