douhan1860 2013-11-12 06:32
浏览 12
已采纳

too long

I would like to implement comet with records fetch from PHP

My PHP will do the following.. at a page call getlog.php

$sql = "select log_description,log_time from log ORDER by log_time DESC";

$result=mysql_query($sql);
if($result == false)
{     die("unable to fetch records."); }

while ($row = mysql_fetch_assoc($result)) {
   $result_output[] = $row;
}

$counter = 1;
foreach($result_output as $row)
{
echo $counter . ".  " $row[log_description];
$counter++;
}

If there is new log, I would want to echo it out in viewlog.php

So it would appear like this in viewlog.php

1. Customer 1 logged in at 12:05.

maybe 5 minutes later

1. Customer 2 logged in at 12:10
2. Customer 1 logged in at 12:05

It maintain a maximum of like lets say 15 records.

The data is fetch from PHP, I read the way to do it is something call "comet" but I just want a simple database fetch which auto refresh e.g every 10 seconds to see if there is new record added to the database and append it to the div.

Is there a easy way to achieve this using AJAX and PHP and not using comet.

Thanks for all the help, greatly appreciate !

Did the following code changes

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
   show_log(){
    var lnk = "fetchlog.php";
    $.ajax({url:lnk,success:function(result){
        $("#log_div").html(result);
    }});
}
   window.setInterval(function(){
  show_log();
}, 10000);
    </script>

</head>
<body>
<div id="log_div"></div>
</body>
</html>

Whats wrong with my code as it doesn't fetch from fetchlog.php

fetchlog.php echo something like this

1. Acct_1 logged to the system.
2. Acct_3  logged in to the system.
3. Acct_2  logged in to the system.
4. Assign permissions on Acct_1.
5. Delete record on table building with id 80

jsFiddle

  • 写回答

1条回答 默认 最新

  • duanouyong4228 2013-11-12 06:38
    关注

    Yes you can use ajax for this and simply update a div in your html. You need to have jquery linked in order to use the below code.

    show_log(){
        var lnk = "link to the viewlog.php file";
        $.ajax({url:lnk,success:function(result){
            $("#log_div").html(result);
        }});
    }
    

    Run the show_log() function every x number of mins. Have your viewlog.php show the last x number of records in the descending order of time. You can update your sql to look like

    $sql = "select log_description,log_time from log ORDER by log_time DESC LIMIT 5 ";
    

    You can use the below inside your javascript to run the function every x number of seconds. In this every 10 seconds.

    window.setInterval(function(){
      show_log();
    }, 10000);
    

    the 10,000 is in miliseconds

    ----- Try the below

    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    
    <script>
    http = getHTTPObject();
    
    function getHTTPObject(){
      var xmlhttp;
    
      if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try {
          xmlhttp = new XMLHttpRequest();
        }catch(e){
          xmlhttp = false;
        }
      }
      return xmlhttp;
    }
    function   show_log(){
        var url = "viewlog.php";
      http.open("GET", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.send(null);
    }
    function handleHttpResponse(){
      if(http.readyState == 4){
        document.getElementById('log_div').innerHTML = http.responseText;
      }
    }
       setInterval ( "show_log()", 5000 );
        </script>
    
    </head>
    <body>
    <div id="log_div"></div>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加