views:

206

answers:

4
+1  Q: 

php progress bar?

Is there a way to show a progress bar in php? I like to let the user know what the php script is doing.

Here's an result example from my website http://www.bestsellprice.com/search.php?itemnum=0&keyword=harry+potter+book

I have a static loading image with some text that says "Loading...", when the page is fully loaded, I hide the loading div. My php script goes out to the amazon api, ebay api and screen scrapes a couple of other sites and instead of a static loading div I would like to update that loading div with the following

Loading amazon.com

//progress amazon, when finished update loading div

Loading ebay.com

etc. etc.

Is this possible to do? if so whats the best approach? Thanks ahead of time for the help!

A: 

The best approach is to have your javascript make ajax calls to the server and find out where it is at, so you can give some feedback to the user.

You can start with having a progress bar for uploading, and then you just continue to show the progress of the application.

James Black
+2  A: 

Use AJAX to ask PHP what it's doing at the moment, and have a queue system that defines what tasks are completed or not. Here's a very basic example:

When the task is started, make it set the following variables:

$_SESSION['isEbayLoaded'] = false;
$_SESSION['isAmazonLoaded'] = false;

Once PHP loads eBay, it would set isEbayLoaded to true, and then AJAX could simply ask PHP if ebay was loaded yet, then amazon, then whatever else you needed. All PHP would have to do is check if those variables were set as true, which would mean that the tasks had been completed.

Bruno De Barros
Are session variables accessible from the main page without reloading that page? That's something you might want to think about.
iddqd
The session variables are being read by the page being called through AJAX, so as they're set in the main script, they should become available for requests after they were set. If not, one could still use temp files, or the database, to store the information, which would then be read when the AJAX request is made. To avoid calling PHP, one could even store a file as .json and then use JavaScript to request the file and process the information on what tasks are complete and what tasks aren't.
Bruno De Barros
A: 

I've accomplished this before with a really simple solution:

ob_flush();

All it required was creating a single div with appropriate class names:

<div class="loading">
    Loading...
</div>

Then, my PHP was basically:

foreach ($sites as $site) {
   printAndFlush(<<<CSS
     <style>
       div.loading {
         background: url($site.png) no-repeat right center;
       }
     </style>
CSS;

   scrapeSite($site);
}

This would use a background style to show which site you would be scraping.

If you must use markup, you can simply print out a new "Loading" div each time (with a unique ID), along with a style to set the previous div's style to "display: none".

Of course, remember that some browsers require at least 1024 bytes (somethings 4k!) before rendering anything.

Eric
Minor detail, but you never close the parenthesis in your PrintAndFlush function. Also, I don't see any reason to print the style tags more than once.
iddqd
Not an actual function, just a pseudo-php function.The reason the style tags were printed more than once were to force their compilation by the browser, as each <style> declaration overwrote the previous style.
Eric
This is one of the few approaches that doesn't require AJAX to function.
Eric
A: 

I would make a request for each site, and use the output from that page (XML, JSON, whatever) to let the other page know what's going on.

For example...psuedo code:

  • gather sites
  • for loop that loads ajax page
  • ajax page returns each request with a status message e.g. - "Loading Amazon...."

That's it!

iddqd