【问题标题】:Auto refresh MySQL Query results in PHP在 PHP 中自动刷新 MySQL 查询结果
【发布时间】:2014-04-23 07:44:45
【问题描述】:

我有这些运行 PHP 函数并返回结果的 DIVS。如何让返回的结果每 X 秒自动刷新一次而不刷新整个页面。

我只想重新运行函数中的 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>

更新:

我用过这段代码:

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

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

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

然后我的所有 div 和 PHP 函数在名为 data.php 的页面上运行,但我的 index.php 页面上没有显示任何内容

【问题讨论】:

  • AJAX 是您所需要的。你不能只用 PHP 来做这件事,因为 PHP 在服务器端运行。
  • 你有 ajax 的例子吗?我只用过 PHP & HTML
  • 如果我使用该代码,我将不得不为每个函数设置一个单独的页面
  • 不,你不会。您只需要一个页面来计算所有值并将它们发送回(已经在所需的 HTML 中,或者以 JSON 之类的格式,您可以解析并将这些部分放在它们所在的位置)。

标签: php mysql sql


【解决方案1】:
<meta http-equiv="refresh" content="5; URL=http://www.yourdomain.com/yoursite.html">

5秒后刷新页面,如果你需要js我也可以发送它

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

对于 ajax,您需要一个以 Json 或 xml 形式返回所有数据的页面。

然后您需要向该页面发出 $.POST 或 $.GET 或 $AJAX 请求,然后对其进行解析,然后更新您的 html 元素。

例子

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

【讨论】:

  • 我不想刷新整个页面
  • 真的,这是正确的做法。多么复杂。你需要 ajax 或 jquery
  • 你有例子吗?
【解决方案2】:
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2018-08-08
    • 2011-10-18
    • 1970-01-01
    • 2023-04-09
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多