dongyixiu3110 2015-02-26 09:56
浏览 31
已采纳

如何通过php或jquery只附加一个id的详细信息

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);
}
  • 写回答

1条回答 默认 最新

  • dshgdhdfcas30210 2015-02-26 11:53
    关注

    Try removing async: true, and check. Also why you are calling waitformsg function again in success section? setTimeout( waitForMsg, 1000 );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)