A simple question about outputting text...

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
skeptikal
Forum Newbie
Posts: 10
Joined: Mon Dec 22, 2003 5:55 pm

A simple question about outputting text...

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
skeptikal
Forum Newbie
Posts: 10
Joined: Mon Dec 22, 2003 5:55 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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]
skeptikal
Forum Newbie
Posts: 10
Joined: Mon Dec 22, 2003 5:55 pm

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