Loop iteration question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Loop iteration question

Post 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.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
Post Reply