I want to append newly added data from sql only once
.
So, I used below script which display newly added data from sql, But it append same data one after one.
That means, If I added test by thisuser
in sql, It's append it continuously 4 or 5 time.
How to make it, one id's description display only once.
My Jquery:
function addmsg(type, msg){
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg.description +" by "+ msg.username +"</div>"
);
}
function waitForMsg(){
$.ajax({
type: "GET",
url: "server.php",
async: true,
cache: false,
timeout:15000,
success: function(data){
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(
waitForMsg,
15000);
}
});
};
$(document).ready(function(){
waitForMsg();
});
server.php
global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
date_default_timezone_set('Asia/Dhaka');
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$results = mysqli_query($dbh,"SELECT * FROM comments WHERE qazi_id='$id' AND date >= '$datetime' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows = mysqli_fetch_assoc($results);
$row[] = array_map('utf8_encode', $rows);
$data = array();
$data['id'] = $rows['id']; /* I want use this id to display once*/
$data['likes'] = $rows['likes'];
$data['username'] = $rows['username'];
$data['img'] = $rows['img'];
$data['description'] = $rows['description'];
$data['parent_id'] = $rows['parent_id'];
$data['date'] = $rows['date'];
//has data
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
sleep(5);
}