I want to update user wall by my below script if new data added in sql within last 15 second.
It's update fine if I just use only one data $data = $row['description'];
in server.php file.
When I am going to array all data in server.php file to display full post and make my script as below, It's display [object Object]
continuously.
So I want to display newly added full post with user img, name, detail etc.
But I have no idea about how to do it.
Please give me a guideline.
server.php
include("../db.php");
global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
date_default_timezone_set('Asia/Dhaka');
//fetch data
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$results = mysqli_query($dbh,"SELECT * FROM comments WHERE qazi_id='1012' AND date >= '$datetime' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows = mysqli_fetch_assoc($results);
$data = array();
$data['id'] = $rows['id'];
$data['qazi_id'] = $rows['qazi_id'];
$data['likes'] = $rows['likes'];
$data['username'] = $rows['username'];
$data['img'] = $rows['img'];
$data['description'] = $rows['description'];
$data['url'] = $rows['url'];
$data['parent_id'] = $rows['parent_id'];
$data['date'] = $rows['date'];
//has data
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
sleep(5);
}
JS Script:
function addmsg(type, msg){
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg +"</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();
});
Html:
<div id="messages">
</div>