Trying to create a progress bar for long PHP script
<p>I'm using <a href="http://w3shaman.com/article/php-progress-bar-script" rel="nofollow">http://w3shaman.com/article/php-progress-bar-script</a> as an example to get a progress bar working for a long database update script and I can't seem to get the thing to function properly.  By necessity I've got some generic HTML in my main executing page and then some client-specific code included and then called.  The progress bar script is a part of the include script though I have the same problem when it's all in the same page too.</p>

<p>What happens is if I comment out the included function call the page loads just fine.  If I allow the function to run I see only the most basic of the layout until the allotted time expires, at which point the page renders completely with the "Process completed" displaying along with a full progress bar instead of the incremental changes as expected.</p>

<p>Here's my main PHP (calling) page:</p>

<pre><code>&lt;?php
// Setup the page header/footer
$pagehtml = writeheader();
$pagehtml .= writemenus(4);
$pagehtml .= '&lt;div class="container maincontent"&gt;
  &lt;div class="row"&gt;';
$pagehtml .= writesidebar();
$pagehtml .= '&lt;div class="tencol last"&gt;
  &lt;div class="content"&gt;';
$pagehtml .= writeinfo();
$pagehtml .= alert_user();
$pagehtml .= '&lt;div class="folder"&gt;
  &lt;div class="controls"&gt;
  &lt;h1&gt;Database Management&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="folderdata"&gt;';
// Conten to be updated
$pagehtml .= '&lt;div id="progress" style="width:100%; border:1px solid #BFBFBF; height:1em;"&gt;&lt;/div&gt;
  &lt;p id="information"&gt;Beginning database import.&lt;/p&gt;';  
// Create footer HTML
$pagehtml .= '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
$pagehtml .= writefooter();
print $pagehtml;

flush();

// Execute the feed
include $_SERVER['DOCUMENT_ROOT'] . '/config/' . $account . '/dbfeed.inc';
dbfeed_execute();
</code></pre>

<p>...and my included file (dbfeed.inc):</p>

<pre><code>&lt;?php
// Main function which executes the entire feed
function dbfeed_execute() {

  // Total processes
  ob_end_flush();
  $total = 5;

  // Loop through process
  for($i=1; $i&lt;=$total; $i++){
      // Calculate the percentation
      $percent = intval($i/$total * 100)."%";

      // Javascript for updating the progress bar and information
      echo '&lt;script language="javascript"&gt;
      document.getElementById("progress").innerHTML="&lt;div style=\"width:'.$percent.';background-color:#ddd;\"&gt;&amp;nbsp;&lt;/div&gt;";
      document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
      &lt;/script&gt;';

      // This is for the buffer achieve the minimum size in order to flush data
      echo str_repeat(' ',1024*64);

      // Send output to browser immediately
      flush();

      // Sleep one second so we can see the delay
      sleep(1);
  }

  // Tell user that the process is completed
  echo '&lt;script language="javascript"&gt;document.getElementById("information").innerHTML="Process completed"&lt;/script&gt;';

}
</code></pre>

<p>I tried basically duplicating the page as written at <a href="http://w3shaman.com/article/php-progress-bar-script" rel="nofollow">http://w3shaman.com/article/php-progress-bar-script</a> and it does indeed work.  However, I need to be able to write the progress bar from within a function and it appears I'm missing something.  Thanks to anyone who can help!</p>
