Jump to content

Recommended Posts

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?

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!";

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.

 

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.

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.

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.