[英]How to append one id's detail only once by php or jquery
I want to append newly added data from sql only once
. 我想only once
从sql附加一次新添加的数据。
So, I used below script which display newly added data from sql, But it append same data one after one. 所以,我使用下面的脚本显示来自sql的新添加的数据,但它一个接一个追加相同的数据。
That means, If I added test by thisuser
in sql, It's append it continuously 4 or 5 time. 这意味着,如果我在sql中添加test by thisuser
,它会连续4或5次附加它。
How to make it, one id's description display only once. 怎么做,一个id的描述只显示一次。
My Jquery: 我的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 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);
}
Try removing async: true,
and check. 尝试删除async: true,
然后检查。 Also why you are calling waitformsg function again in success section? 还有为什么你在成功部分再次调用waitformsg函数? setTimeout( waitForMsg, 1000 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.