Page 1 of 1

Printing running status messages/progress bars

Posted: Fri Feb 16, 2007 5:54 pm
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

Posted: Fri Feb 16, 2007 5:57 pm
by Weirdan
flush() the output when you need to send the chunk of html you just generated.

Posted: Fri Feb 16, 2007 6:03 pm
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?

Posted: Fri Feb 16, 2007 6:05 pm
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

Posted: Fri Feb 16, 2007 6:13 pm
by Kieran Huggins
ob_flush() might be your best bet. Read: http://ca3.php.net/manual/en/ref.outcontrol.php

Posted: Fri Feb 16, 2007 6:53 pm
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.

Posted: Fri Feb 16, 2007 6:57 pm
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...

Posted: Fri Feb 16, 2007 7:04 pm
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.

Posted: Fri Feb 16, 2007 7:26 pm
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...

Posted: Fri Feb 16, 2007 7:32 pm
by superdezign
Well the EXACT same thing could be performed in JavaScript. I was just showing the concept using CSS.

Posted: Fri Feb 16, 2007 7:44 pm
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()

Posted: Fri Feb 16, 2007 7:54 pm
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.

Posted: Sat Feb 17, 2007 4:43 am
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.