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!
I've seen scripts that output text to a page while it is still being processed. Let's say it has a "for" loop with 10 iterations; they print "OK" every time each iteration ends. However, if I try the same, it will just proccess the whole loop, and then print 10 "OK"s all at once.
I know this must be something stupid, but... how do I enable that kind of "per iteration" output? I'm creating a script that will take long to do what it's supposed to and it would be helpful to print some messages that at least tell the user what the system is up to while it proccesses....
generally the data is batched into the output with php.. you can ask the engine to [php_man]flush[/php_man]() but that's only supported on certain servers.. And even that, gets batched..
As you predicted, this didn't work at all... Does anyone know if there is some setting I could change in my Apache and PHP to allow flushing correctly? I run a test server in my office, so I could easily make any necessary changes on the server settings to get this running.
<?php
print "[";
for($i = 0; $i < 100; $i++){
$spaces.=" ";
} // for
//and then
for($i = 0; $i < 10; $i++){
for($ii = 0; $ii < 200000; $ii++){
//do something slow here
} // for
print "$spaces|";
flush();
} // for
print "]";
/*
but this somethin may be not really what you expect in
a progress bar, as it prints spaces (although rendered as single by the browser) between the bar units..
you can solve this using
$spaces.="<!-- bufferme -->";
/*
print "[";
for($i = 0; $i < 100; $i++){
$spaces.="<!-- bufferme -->";
} // for
//and then
for($i = 0; $i < 10; $i++){
for($ii = 0; $ii < 200000; $ii++){
//do something slow here
} // for
print "$spaces|";
flush();
} // for
print "]";
//which looks nice as a progress bar..
?>
there are a lot more useful comments you shuld look at
on [php_man]flush[/php_man]
I took a look at the page and I realized that the whole problem was the minimum buffer required to flush. To get around this, I used an ugly method (but it worked): I added some HTML commented "fake" output to make the page flush more often. So it's working the way it's supposed to now
Thanks for both of you for the help, you can consider this one solved too