Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying a Progress bar between code snippets

Status
Not open for further replies.

JMay0816

Programmer
Mar 1, 2007
34
US
I would like to display a progress bar in between my code statements. Is this possible with PHP and if so, can someone lend a hand?

Example:

Checking for pending requests:
<progress bar>
- display if any found
Checking for overdue requests:
<progress bar>
- display if any found



 
the answer is ... not really - at least with php.

i'd look at using javascript to inform the user of server wait times and then fire off the requests for action using XMLHTTPREQUEST objects. this may be one of those occasions where the object should be used in synchronous mode rather than async. post in the js forum for more info. the object can interact with a php server just fine, of course.
 
or how about something like the following:


Checking status.....

each time via a counter or something another "." gets added

or is the same thing?

I have been experimenting with some things but it only produces an end result not the expected visual effect.
 
That's because PHP does not run on the client, only on the server. And unlike JS and normal applications it is non continuous. Meaning that By the time you see the page in your browser, PHP is all done and finished. Its not running any more. so anything you place between code snippets will have to wait until PHP is done to be displayed.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
same thing. php is a stateless application. it produces output and sends the output to the client. there is no client server interaction outside of page refreshes and use of xmlhttpobject thru javascript.

what you're trying to do is perfectly normal and easy to manage: use js and xmlhttpobjects to do so. the mechanics are only one step above trivial.
 
A trick I sometimes use n other languages is to start my HTML at the top of the page and output my entire head, the body tag, and a div with a div inside it. Then throughout later blocks of code I output a chunk of javascript that basically gets the inner element and increases it's width. The biggest part of this trick is to flush() or ob_flush() after each chunk of javascript. This causes it to get sent to the browser immediately.
The last important piece of this is to hide the bar once the page has fully loaded. You can do this by changing it's CSS display property to none when the onLoad event occurs.

So basically something along the lines of:
Code:
<?
// outputs javascript to update bar by num% and flushes buffer
function UpdateStatusBar($num){
   echo "<script language='javascript'> docuemnt.getELementById('loadingBar').style.width = $num%; </script>";
   flush();
}

?>
<html>
<head>
<title> My Slow Page</title>
</head>
<body onLoad="document.getElementById("statusBar").style.display='none';">
<div id="statusBar" style="border: 1px solid black; width: 300px" ><div id="loadingBar" style="width: 1%; background-color: green;">&nbsp;</div></div>
<?
// lots of slow code

UpdateStatusBar(25);

// lots of slow code

UpdateStatusBar(50);

// lots of slow code

UpdateStatusBar(75);

// lots of slow code

UpdateStatusBar(100);

// all done

It's not a perfect solution, as constantly flushing the buffer can cause a performance slow down and you might have to use something more extensive to force flushing on your system, but it works without getting too crazy with javascript.

Warning: I wrote the above code completely in this window from scratch, so it may have smoe errors, typoes, etc.

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top