Page 1 of 1

A simple question about outputting text...

Posted: Fri Aug 13, 2004 6:21 pm
by skeptikal
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....

Thanks! :)

Skeptikal

Posted: Fri Aug 13, 2004 7:43 pm
by feyd
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..

Posted: Sat Aug 14, 2004 3:36 pm
by skeptikal
Thanks for the answer, feyd.

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.

Regards,

Thiago

Posted: Sun Aug 15, 2004 12:58 am
by John Cartwright
php.net

Code: Select all

<?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]

Posted: Sun Aug 15, 2004 2:41 am
by skeptikal
Phenom, thanks for your help and for the link...

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 :)

Regards,

Thiago