Printing running status messages/progress bars

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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Printing running status messages/progress bars

Post by mjseaden »

Hi all,

I've tried creating a web script with PHP code interspersed with JavaScript, like so:

Code: Select all

<?php
echo 'Woo some PHP code!';

?>
<script type="text/javascript">
document.write('Hello this is some text')
</script>
<?php
echo 'Woo some more PHP code!';
?>
Of course, the HTML for this script is not sent to the client until its execution is completed, and so all of the JavaScript gets executed all at once, instead of the client running the server-side code, then displaying the javascript document.write, then more PHP code, then javascript document.write, etc. etc.

Is this a job for AJAX (i.e. do I have to use the XmlHttpRequest to do what I want to do)?

Many thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

flush() the output when you need to send the chunk of html you just generated.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Just make sure you're not going past your max execution time (default 30s) - otherwise you may consider using ajax.

What are you reporting the progress of exactly?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Kieran.

Thanks for both responses. I'm running an installation script which I am developing at the same time as the rest of the site so that it can be generated properly with changes.

It says "Importing database...", "Generating directory structure...", "Unzipping images...", etc. etc.

I just want to output these messages after each piece of server-side code has been completed. I can use ini_set to temporarily increase the permitted execution time for the installation script.

Many thanks
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

ob_flush() might be your best bet. Read: http://ca3.php.net/manual/en/ref.outcontrol.php
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

An interesting alternative is utilizing Javascript's innerHTML and/or CSS' display: none;

Just insert Javascript commands as your script runs.

Code: Select all

<span style="display: none;">Loading...</span>
<?php
//some code
?><style type="text/css">#loading{display: inline;}</style><?php
$foo->bar();
?><style type="text/css">#loading{display: none;}</style>
Just an interesting thought.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

superdezign wrote:An interesting alternative is utilizing Javascript's innerHTML and/or CSS' display: none;

Just insert Javascript commands as your script runs.

Code: Select all

<span style="display: none;">Loading...</span>
<?php
//some code
?><style type="text/css">#loading{display: inline;}</style><?php
$foo->bar();
?><style type="text/css">#loading{display: none;}</style>
Just an interesting thought.
That won't work...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Because I forgot to give the span id="loading"? That was a typo :-p

Or does php not output step by step... That'd make sense.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

superdezign wrote:Or does php not output step by step... That'd make sense.
That, and I don't believe that css can be used in the same way as javascript can...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well the EXACT same thing could be performed in JavaScript. I was just showing the concept using CSS.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If it's a loading indicator, you should use ajax as the indication is that the page is waiting for something.

If it's a short-term installer progress, I would stick with ob_flush()
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

You may want to look at how PHPBB handles their installation. If I recall correctly, one of the install pages has a whole stream of periods (.) being displayed on the page (probably after each SQL query). Whatever method they're using (probably ob_flush) can surely be adapted to your purposes.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Okay, thanks chaps, I'll use ob_flush().

I will probably develop a progress indicator class at some point, when that happens, I'll post how I do it here.
Post Reply