citricsquid Posted August 25, 2009 Share Posted August 25, 2009 Hi, I have a while loop that pulls data and then plays with it, this process takes about 0.5 seconds per result and obviously having 20 results without any output will make the user frustrated and think it's broken, it's also not very pretty. How would I go about outputting as the script runs? I've tried a few things with ob_flush(); and it doesn't work. while(){ echo 'results'; } I want the output to be after each echo of "results". Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/ Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 im not really sure what you are trying to do... if you want to output something then... well... output it. What are your results that you want to output? what exactly does this loop do that currently prevents you from outputting things? you could always do echo "Working on it" before the loop and "done" after like echo "Executing script. Please wait..."; while(){ #your code #goes here } echo "Script Complete!"; Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-905981 Share on other sites More sharing options...
citricsquid Posted August 25, 2009 Author Share Posted August 25, 2009 It performs an action and returns the result. Now, if this script takes 20 seconds to run then you'll have 20 seconds of nothing loaded until the output is generated by PHP. So I want to use PHP output buffering to output the script AFTER each result. It's definitely possible, I just can't work out the proper way to do it: So after EACH result it returns the output from that result, NOT after the entire script has run, which is the default action of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-905983 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 It performs an action and returns the result. thats the worst description of any code I have ever read ever... why don't you post what you have? Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-905985 Share on other sites More sharing options...
citricsquid Posted August 25, 2009 Author Share Posted August 25, 2009 It performs an action and returns the result. thats the worst description of any code I have ever read ever... why don't you post what you have? What's hard to understand? I have a while loop that runs for mysql results, it takes each result and performs some "work" on it, let's say for example the script adds "hello" to this result. Instead of the entire loop running and then returning the script output I want it to output after each result is generated. I don't see how that's hard to understand, maybe I'm explaining it funny but it makes perfect sense to me. I want to use output buffering to return a result after each "piece" of the loop, not after the entire loop. http://www.icemelon.com/tutorials/20/Output_While_Script_is_Still_Running.htm Basically that, but this code doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-905988 Share on other sites More sharing options...
PFMaBiSmAd Posted August 25, 2009 Share Posted August 25, 2009 Getting sequential output to actually be output by php and the web server and to get it to be rendered in the browser requires that you satisfy, bypass, or disable all the output buffering, compression, and minimum block length requirements (varies by browser type) of php, the web server, and the browser. And since this varies by web server/php configuration, even if your get it to work on your current server or development system, it will likely not work on the next server or your live server. Web servers/browsers and the whole Internet were not designed to have the output from one HTTP request sent to the browser and displayed as it is produced. There are a bunch of notes the php.net documentation for the output buffering that you will need to read and satisfy for each different browser you expect visitors to use on your site. The way you should display a status that you can update is by using AJAX to periodically (once every few seconds or as appropriate for your application) get the current status from the web server and update a <div> on your page. Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-905990 Share on other sites More sharing options...
citricsquid Posted August 25, 2009 Author Share Posted August 25, 2009 I've fixed it, I had a comment in the wrong place... I've checked it in every modern browser I have and it works perfectly in each, why is there a problem with doing this? I sort of understand what you're saying but I don't see how outputting as the data is gathered is going to cause problems, but it works perfectly for me and the numerous other websites I've seen it done on. Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-905995 Share on other sites More sharing options...
PFMaBiSmAd Posted August 25, 2009 Share Posted August 25, 2009 Because most web sites use javascript to display the "Executing script. Please wait..." type messages that get replaced when the actual content is done. See the example at this link - http://www.dynamicdrive.com/forums/archive/index.php/t-38639.html Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-906042 Share on other sites More sharing options...
citricsquid Posted August 25, 2009 Author Share Posted August 25, 2009 Because most web sites use javascript to display the "Executing script. Please wait..." type messages that get replaced when the actual content is done. See the example at this link - http://www.dynamicdrive.com/forums/archive/index.php/t-38639.html I know how to do that, I use it in other sections of a website but for this it isn't suitable. I need the data to be displayed as it's parsed, I could use ajax but for each request that's going to add at least an extra 300ms of execution which really doesn't need to exist and would be frustrating. Quote Link to comment https://forums.phpfreaks.com/topic/171818-using-ob_flush-to-output-as-the-script-runs/#findComment-906068 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.