2012-06-28 107 views
0

我有以下的foreach循環:動態加載foreach循環

   <?php 
      ob_start(); 
      foreach ($streams as &$stream) { 
       $array = stream($stream); 
      ?> 
       <a href="livestreams.php?stream=<?=$stream;?>"> 
        <div class="channel-preview" style="background-image:url(<?=$array[2];?>);"> 
         <div class="container"> 
          <hgroup> 
           <h1><?=$array[4];?></h1> 
           <h2><?=$array[3];?></h2> 
          </hgroup> 

          <div class="meta"> 
           <span class="live-viewers">Live Viewers:</strong> <?=$array[1];?></span> 
          </div> 

          <span class="game-badge starcraft-2" title="Starcraft 2"><span class="hide">Starcraft 2</span></span> 
         </div> 
        </div> 
       </a> 
      <?php 
        ob_flush(); 
        flush(); 
       } 
       ob_end_flush(); 
      ?> 

請注意,這只是整個腳本的一部分。它的問題是,它加載相當緩慢。我試圖使用ob_flush();如你所見,加快速度,但沒有太大的區別。

我的問題是整個頁面的加載時間。頁面甚至需要一些時間才能在瀏覽器中顯示。在頁面已經被渲染的時候,是否有可能使用AJAX加載循環?或者還有其他方法嗎?

編輯:

的正被循環,雖然從外部服務器接收,這是最有可能是什麼原因造成的延遲。我早該說過了。

+0

請閱讀更多關於潮紅和沖洗問題的建議變通辦法:http://php.net/manual/en /function.flush.php –

+0

我想你應該優化什麼stream()而不是修補ajax /客戶端方式,輸出緩衝區等。無論如何,這些數據是什麼,給一些信息。 – 2012-06-28 20:03:08

+0

我已更新該問題。 – Lazze

回答

0

喜歡這樣一個簡單的一招可能會奏效[上php.net的建議]

<?php 
// All you need is 256 spaces first 
echo str_repeat(" ", 256)."<pre>"; flush(); 

// and ANY TAG before \r\n 
echo "working...<br/>\r\n"; flush(); sleep(1); // this in cycle 
?> 

編輯:另外加入沖洗後的延遲可能解決問題:

usleep(50000);// delay minimum of .05 seconds to allow ie to flush to screen 

請仔細閱讀更多關於沖洗和建議在這裏沖洗問題的變通辦法:http://php.net/manual/en/function.flush.php