Property in Brazil - Debt Consolidation - Wordpress Themes - Business in 2007 - Web Hosting

PDA

View Full Version : How do I force output to the screen during a while loop


sm9ai
Feb 9th 2009, 8:38 am
Hi All,

I have a PHP script that runs in a while loop which takes about 10 minutes to run.

I want to output updates to the screen whilst this is running but it waits until the script is finished then outputs it all in one go.

Is there anyway to make it output as its going along?

Thanks

Matt

steelaz
Feb 9th 2009, 8:49 am
I don't think it's possible. Server sends page to the browser after page is generated.

I had similar problem with PHP task running almost an hour. To avoid timeouts and other problems, I would create new task request in database and initiate cron job to start the process. While working on the task, cron job would update status in database. When task request is created, I redirect browser to the page that is checking on task status every few seconds using AJAX.

mytor
Feb 9th 2009, 8:49 am
ob_start();
while ($status == 'single') {
echo 'having fun ... ';
ob_end_flush();
flush();
$status = go_date();
}

sm9ai
Feb 9th 2009, 10:49 am
ob_start();
while ($status == 'single') {
echo 'having fun ... ';
ob_end_flush();
flush();
$status = go_date();
}


Perfect, just what I needed. Many Thanks - Rep added.

steelaz
Feb 9th 2009, 12:08 pm
Ah.. forgot about output buffers. Thanks, mytor, good stuff.

wmtips
Feb 9th 2009, 12:48 pm
Just insert
ob_implicit_flush(true);
in the beginning of the script..