Page 1 of 1

Loop iteration question

Posted: Thu Apr 14, 2005 9:28 pm
by spacebiscuit
Hi,

I am running a loop that will take a considerable amount of time to execute. At the end of each iteration I am printing a success message to the screen. At present as the loop executes I am left with a white screen and then when it completes I receive all of the success messages in one big hit.

How can I do this so that the success message is printed to the screen in real time, ie each time the loop iterates. I'd like the list to grow as the loop executes.

Is this possible?

Thanks,

Rob.

Posted: Thu Apr 14, 2005 9:44 pm
by SystemWisdom
Sounds like you have output buffering on...
You should check out the flush() function, maybe that will help you..

But note (from php.net):
Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

Posted: Fri Apr 15, 2005 5:31 am
by spacebiscuit
Thanks for the tip! If ound that this piece of code worked just after the text I want to output:

Code: Select all

flush();
ob_flush();
usleep(50000);
Many thanks,

Rob.

Posted: Fri Apr 15, 2005 5:56 am
by spacebiscuit
It seems the above code only worked after I refreshed the page. On the first execute the flush didn't work.

I found this elsewhere and now it works perfectly:

Code: Select all

<?=(str_repeat(' ',256)."n")?>
Add this to the first line of the file and it will work.

Rob.