I'm having a little trouble fetching the newest bit of data. All I'm wanting is to find if there is any new data in the database and print the number of records back into my page. I have the ajax working and it has the last notification_id that it sends over to the php. I just need to get the php to select any new data, send it back and print it out.
I haven't yet tried polling, but will look into it when I have this working and understand it a bit better.
<? $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_id=mysqli_fetch_array($chant))
{
?>
<script type="text/javascript">
setInterval(function(){
var notification_id="<?php echo $notification_id['notification_id'] ;?>"
$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,
dataType:"json",
cache: false,
success: function(data){
$(".mess"+notification_id).prepend("<span class'mess' id='mes'>"+response['notification_id']+"</span>");
}
});
},20000);
</script>
<? }?>
echo '';
PHP viewajax.php - changed it to JSON - but getting {"notification_id":null} in the response
<?php
session_start();
include"database.php";
if(isset($_GET['notification_id']))
{
$id=$_GET['notification_id'];
$user1_id=$_SESSION['id'];
$json = array();
$com=mysqli_query($mysqli,"select notification_id from notifications where notification_id>'$id' AND notification_targetuser='$user1_id' AND notification_status=1");
$resultArr = mysqli_fetch_array($com);
$json['notification_id'] = $resultArr['notification_id'];
mysqli_free_result($com);
echo json_encode($json);
}?>
If you want new data, why don't you just do notification_id > '$id'
instead of notification_id='$id'
?
I assume notification_id
is an integer index column and $id
is the last id front-end received.