FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Outputting Data to the Browser Before Script is finished





Star Wars Fanatic
I want to be able to display something in the browser before the script is finished, is there any way to do this?

For example, here is a script:

Code:
<?php
echo "1";
echo "2";
echo "3";
echo "4";
echo "5";
?>


And I want you to see the page load and display the numbers 1-5.

The script I need this for actually takes so long to load because it is opening several web pages and pulling information from them, and I don't want people to stop the loading thinking there is something wrong, I want the script to display a little update every few seconds or something...
Agent ME
What you posted will do that, if you put the actual code between/after the echo's.
Star Wars Fanatic
Nope, my code doesn't do that, I can send an echo, and it won't display on the screen until the script has finished.
Dougie1
Agent ME wrote:
What you posted will do that, if you put the actual code between/after the echo's.

What it would do would be echo each thing one after the other on the page. Each one would load though so what you say is right.

You could always do something like this:

Code:
<?php
echo "1";
sleep(5);
echo "2";
sleep(5);
echo "3";


And then you will see that it echos one after the other. If what you mean is write one then overwrite it then put the other I think you will have to use AJAX, and make your php script it calls to echo something different each time it is called to.

With AJAX you can just get a simple line of code which will output whatever the php script outputs and you could insert a small bit of code like this:

Code:
if (xmlHttp.readyState==1 || xmlHttp.readyState==2 || xmlHttp.readyState==3)
{
document.getElementById("txtHint").innerHTML="loading...";
}


Just learn a bit of AJAX. Even if you don't know any javascript it is not too difficult and it makes web programmes a lot nicer for the user.
shamil
You can do it by flush();
flush command pushes the current buffered output to the browser.

But u must be careful when using tables. Because tables must be complete in order to appear in the browser. Table will not show until it's tag is closed.

http://www.hudzilla.org/phpbook/read.php/13_9_0
http://www.php.net/flush
Star Wars Fanatic
Well, flush() wasn't working for me, and neither was sleep(), but after changing it to
Code:
ob_flush();flush();
It worked perfectly, thanks for your help.
bgillingham
Star Wars Fanatic wrote:
Well, flush() wasn't working for me, and neither was sleep(), but after changing it to
Code:
ob_flush();flush();
It worked perfectly, thanks for your help.

In IE, flush() will not cause the buffer to be forced to the browser, unless the buffer is already more than 255 characters. This is Microsoft's idea of how to handle the buffer in their browser. In the event that you want to get your results at the first flush, you need to start out your document by passing 255 spaces - and then flush will have enough characters (to make it worth the effort - for microsoft).
Related topics
How To : Secure Your PHP Website
do you like my site design ?
Bluetooth
Psuedo Frames in IE
Don Brown - The Da Vinci Code FOR and AGAINST
writing COM components in Perl
AJAX tutorial [2nd part now updated]
PERL - post data from one script to another (Without LWP)
Browser Version detection and redirection
header_already_sent!
*Super Duper Urgent* "Headers Already Sent"
Header problems
Free Download Email Signup
Table Help / Javascript dopdown menu
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.