我可以使用我已经拥有的PHP代码自动刷新我的div

时间:2012-02-03 11:20:13

标签: php refresh reload

有没有办法每分钟左右制作一个div autorefresh,而不是我的整个页面?因为每分钟重新加载页面需要很长时间。 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;
}
?>

5 个答案:

答案 0 :(得分:3)

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

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

OR,更简单:

$('#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>

答案 1 :(得分:2)

您需要使用JavaScript更新AJAX或使用div进行iframe请求。

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

如果您打算使用AJAX,那么您将需要使用setInterval()函数重复一个实际调用AJAX请求并更新div内容的函数调用。但是,最好使用setTimeout()并让函数调用自身,当然也可以在X秒后使用setTimeout()。原因是,通过使用setInterval(),即使最后一个操作尚未完成,JavaScript仍将继续运行。但是如果你要使用setTimeout() JavaScript将等待最后一个函数调用完成,然后再转到下一个函数。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

你想要刷新div,你将需要ajax,最简单的解决方案就是使用jQueryload()

答案 4 :(得分:0)

* 在php中不可能刷新特定的div只有整个页面刷新才有可能试试这个,我想你在AJAX中做它可能使用settimeout刷新*

//在php中我们有

标题( “刷新:5; URL:tvinfo.php”);