开发者

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!


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.


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.


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜