severndigital Posted January 21, 2009 Share Posted January 21, 2009 ok .. so i want to have a message display to the browser after each instance of the for loop is complete. is there a way to do this in PHP or will i have to use Javascript to make it happen?? example $arrayList = array(1,2,3,4,5); for($i = 0;$i < count($arrayList); $i++){ //do something here. // then echo completion information echo 'Process: ' . $arrayList[$i] . ' Complete <br />'; } it works but shows all the echos after the script runs ... is there a way to echo the string directly after the loop runs ?? Thanks, -C Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/ Share on other sites More sharing options...
GingerRobot Posted January 21, 2009 Share Posted January 21, 2009 By default, PHP is buffered. You can force the output to be flushed to the browser with flush, however. Unless you're doing some pretty intensive stuff or you call sleep you're not going to notice it though. Also, if you want to test it, you'll need to echo and flush some white space to the browser or something - a lot of browsers have their own buffering and require a certain amount of data before they'll display anything. Edit: For example: <?php echo str_repeat(' ',256); flush(); for($x=0;$x<5;$x++){ echo $x.'<br />'; flush(); sleep(1); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/#findComment-742296 Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 $arrayList = array(1,2,3,4,5); for($i = 0;$i < count($arrayList); $i++){ //do something here. // then echo completion information echo 'Process: ' . $arrayList[$i] . ' Complete <br />'; } ob_flush(); flush(); It will only work on some browsers. I know it works on FF, unsure about any others. Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/#findComment-742299 Share on other sites More sharing options...
GingerRobot Posted January 21, 2009 Share Posted January 21, 2009 It will only work on some browsers. I know it works on FF, unsure about any others. Also, if you want to test it, you'll need to echo and flush some white space to the browser or something - a lot of browsers have their own buffering and require a certain amount of data before they'll display anything. Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/#findComment-742304 Share on other sites More sharing options...
severndigital Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks, I will be messing around with this after lunch. BTW .. i am using this to batch compress directories, so it will be fairly intensive. It's for a specialized project though, that will be running only locally on the machine. Thanks again -C Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/#findComment-742337 Share on other sites More sharing options...
severndigital Posted January 23, 2009 Author Share Posted January 23, 2009 still can't seem to get this to work. I tried using the flush() command and also tried adding white spaces before the message (256 of them to be exact) still no luck. any other ways i can get this to work?? using javascript or something?? Thanks, C Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/#findComment-744696 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 There is at least one more factor, if your server/browser negotiate to use compression to send the requested page. Php, web servers, and browsers all (can) do buffering and compression. To get this to work you must statisfy/flush all the buffers and prevent any compression. Web servers and browsers are not designed to have one request for a page to be output and displayed as discrete pieces. The sure fire way to get this to work is to have the browser make timed requests to a page on the server and that page returns the available information that is then displayed in the browser. This is typically done using AJAX, but you could just have the page refresh itself at timed intervals. Quote Link to comment https://forums.phpfreaks.com/topic/141785-echo-message-after-each-for-loop-run/#findComment-744708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.