【问题标题】:can i autorefresh my div using the php code i already have我可以使用我已经拥有的 php 代码自动刷新我的 div
【发布时间】:2012-02-03 11:20:12
【问题描述】:

有没有办法让某个 div 每分钟左右自动刷新一次,而不是我的整个页面?因为每分钟重新加载页面需要很长时间。 div本身是可以改变的重要部分。

<div id="livetable">
<?php loadpage(); ?>
</div>

<?php
define('ELEMENT_CONTENT_ONLY', true);
define('ELEMENT_PRESERVE_TAGS', false);

function value_in($element_name, $xml, $content_only = true) 
{
    if ($xml == false)
    {
        return false;
    }
    $found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.'</'.$element_name.'>#s', $xml, $matches);
    if ($found != false) 
    {
        if ($content_only) 
        {
            return $matches[1];  //ignore the enclosing tags
        }
       else 
       {
            return $matches[0];  //return the full pattern match
        }
    }
    // No match found: return false.
    return false;
}

function loadpage()
{
      echo "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 95%\" >";
      echo "<tr class=\"info-row\" bgcolor=#252525 style=\"color:white;  height: 15px;\">";
      echo "<td style=\"width: 14%; height: 10px; padding-left: 5px;\"><b>Preview</b></td>";
      echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Live</b></td>";
      echo "<td style=\"width: 36%; height: 10px; padding-left: 5px;\"><b>Stream</b></td>";
      echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Viewers</b></td>";
      echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Time online</b></td>";
      echo "</tr>";
      addrow(107473,10,"Osuryn","Osuryn is streaming random games live",false);
      addrow(210320,28,"Dennojj","Dennojj is streaming PS3 games",true);
      echo "</table>";
}

function addrow($streamID, $streamPage , $streamName , $streamSlogan, $odd)
{
     if ($odd)
     {
            echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7>";
     }
     else
     {
            echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF>";
     }
     echo "<td style=\"width: 14%;\"><img src=\"http://img.hw.own3d.tv/live/live_tn_".$streamID."_.jpg\" style=\"height: 72px;\" \></td>";
     echo "<td style=\"width: 10%; padding-left: 5px;\"><br><br><b>".getLiveStatus($streamID)."</b></td>";
     echo "<td style=\"width: 36%; vertical-align: top; padding-top: 6px; padding-right: 6px;\">";
     echo "<div><br><a href=\"http://brokendiamond.org/?q=node/$streamPage\">$streamName</a></div>";
     echo "<div style=\"padding-top: 6px; font-size: 11px;\">$streamSlogan</div>";
     echo "</td>";
     echo "<td style=\"width: 10%; padding-left: 5px;\"><br><br>".getLiveViews($streamID)."</td>";
     echo "<td style=\"width: 10%; padding-left: 5px;\"><br><br>".getOnTime($streamID)." minutes online</td>";
     echo "</tr>";
}

function getLiveStatus($streamID)
{
    $request =  'http://api.own3d.tv/liveCheck.php?live_id='.$streamID;
    $arg = '240';

    $session = curl_init($request.$arg);

    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);

    if (preg_match("/true/",$response, $result)) 
    {
        $streamStatus="Live";
    } 
    else 
    {
      $streamStatus="Offline";
    }
    return $streamStatus;
}

function getLiveViews($StreamID)
{
    $request =  'http://api.own3d.tv/liveCheck.php?live_id='.$StreamID;
    $arg = '240';

    $session = curl_init($request.$arg);

    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);

    $viewStatus =value_in('liveViewers', $response) + "";

    return $viewStatus;
}

function getOnTime($StreamID)
{
    $request =  'http://api.own3d.tv/liveCheck.php?live_id='.$StreamID;
    $arg = '240';

    $session = curl_init($request.$arg);

    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);

    $onStatus =value_in('LiveDuration', $response) + "";

    return $onStatus;
}
?>

【问题讨论】:

标签: php refresh reload


【解决方案1】:

不只是使用 PHP,不。但是您可以使用 AJAX 从外部文件中检索数据并使用 JavaScript 更新 div 的内容。使用 jQuery:

$.get('/path/to/php/script', function(data) {
  $('#selector').html(data);
});

或者,更简单地说:

$('#selector').load('/path/to/php/script');

编辑 - 澄清:

文件“ajax.php”:

<?php

// Include your functions here
echo loadpage();

HTML 文件:

<div id="content"></div>
<script>
  $(document).ready(function() {
    $("#livetable").load("ajax.php");
  });
</script>

【讨论】:

  • 是否可以使用我的加载页面功能来代替全新的脚本?它只会变成复制粘贴,但它会让我更容易
  • 是的。只需在您通过 ajax 调用的页面上包含该函数,并将该函数的响应作为页面输出返回。
  • @Gary Chambers,我需要在#selector 位置放什么? .load 可以是 ('loadpage()');对吗?
  • 嗯,我试过了,但没用:s 它什么也没显示
  • 您的 loadpage 函数似乎已经在回显输出。从我上面包含的 ajax.php 行中删除 echo。
【解决方案2】:

您需要使用 JavaScript 发出 AJAX 请求,该请求将更新 div 或使用 iframe。

最简单的方法是使用iframe。基本上,您需要做的是使用iframe 加载div 的内容,并修改iframe 中使用的PHP 代码以在X 秒后重新加载页面。使用 PHP header() 函数应该很容易。

如果您打算改用 AJAX,则需要使用 setInterval() 函数重复一个函数调用,该函数调用实际上会发出 AJAX 请求并更新 div 的内容。但是,使用setTimeout() 并让函数自己调用可能是个好主意,当然也可以在X 秒后使用setTimeout()。这样做的原因是,通过使用setInterval(),即使最后一个操作尚未完成,JavaScript 也会继续运行。但如果你使用setTimeout(),JavaScript 将等待最后一个函数调用完成,然后再继续下一个。

【讨论】:

    【解决方案3】:

    您可以使用meta refresh 轻松刷新整个页面。

    您只想在 div 上刷新,您将需要 ajax,可能最简单的解决方案是使用 jQuery 或只是 load()

    【讨论】:

      【解决方案4】:

      *在 php 中无法刷新特定的 div,只能刷新整个页面,试试这个,如果你想在 AJAX 中这样做,可以使用 settimeout refresh *

      //在php中我们有

      header("刷新:5;url:tvinfo.php");

      【讨论】:

      • 这将刷新整个页面,而不仅仅是 div
      • @DamienPirsy ,是的,可以使用 jquery $('div').get());setTimeout(function(){window.location.reload=2000;});
      【解决方案5】:

      服务器不允许通过正常的 html 传递向客户端推送内容。 使用客户端 ajax-request 获取 div 的新内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 2013-11-07
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多