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 );

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行