简体   繁体   English

自动刷新 MySQL 查询结果在 PHP

[英]Auto refresh MySQL Query results in PHP

I have these DIVS which run PHP functions and return results.我有这些运行 PHP 函数并返回结果的 DIVS。 How can i make the returned results automatically refresh every X seconds without refreshing the whole page.如何使返回的结果每 X 秒自动刷新一次而不刷新整个页面。

I just want to re-run the PHP/MySQL queries that are in the functions我只想重新运行函数中的 PHP/MySQL 查询

<div class="container">

<div class="box"><h2>Callers Waiting</h2><?php echo CallersWaiting($queue_name, date('Y-m-d H:i:s', strtotime('-1 hour'))); ?></div>

<div class="box"><h2>Average Hold Time</h2><?php echo AverageHoldTime($queue_name, $date); ?></div>

<div class="box"><h2>Longest Wait Time</h2><?php echo LongestWaitTime($queue_name, $date); ?></div>

<div class="box"><h2>Shortest Wait Time</h2><?php echo ShortestWaitTime($queue_name, $date); ?></div>

</div>

UPDATE:更新:

I have used this code:我用过这段代码:

<div class="container"></div>

<script type="text/javascript">
    $(document).ready(function(){
      refreshTable();
    });

    function refreshTable(){
        $('.container').load('data.php', function(){
           setTimeout(refreshTable, 2000);
        });
    }
</script>

then all my divs and PHP Functions run on a page called data.php but nothing is showing on my index.php page然后我所有的 div 和 PHP 函数在名为 data.php 的页面上运行,但我的 index.php 页面上没有显示任何内容

<meta http-equiv="refresh" content="5; URL=http://www.yourdomain.com/yoursite.html">

regresh page after 5 sec and if you need js i can send it too 5 秒后重新刷新页面,如果您需要 js,我也可以发送它

setTimeout(function(){
   window.location.reload(1);
}, 5000);

For ajax you need a page that return all you data in Json or xml form.对于 ajax,您需要一个以 Json 或 xml 形式返回所有数据的页面。

then you need to make $.POST or $.GET or $AJAX request to that page and then parse it and then update your html elements.然后您需要向该页面发出 $.POST 或 $.GET 或 $AJAX 请求,然后解析它,然后更新您的 html 元素。

example例子

http://danielnouri.org/notes/2011/03/13/an-ajax-page-update-mini-tutorial/ http://danielnouri.org/notes/2011/03/13/an-ajax-page-update-mini-tutorial/

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('.container').load('data.php');
}, 2000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
});
// ]]></script>

<div class="container">Loading data ...</div>

If you need to refresh a page when clicking on a div, you can use AJAX Poll , but if you want to update the element every specific period, your code must be like so如果您需要在单击 div 时刷新页面,您可以使用AJAX Poll ,但是如果您想在每个特定时期更新元素,您的代码必须是这样的

$(document).ready(function () {
    setInterval(function () {
        $.ajax({
            url: 'data.php',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                $("#your-element-id").html(data.your_data);
            },
            error: function (ERROR) {
                console.log(ERROR);
            }
        });
    }, 2000); // trigger this function every 2 sec.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM