Php progress bar?
Is there a way to show a progress bar in php? I like to inform the user about what the php script is doing.
Here is an example result from my site http://www.bestsellprice.com/search.php?itemnum=0&keyword=harry+potter+book
I have a static loading image with text that says "Loading ...", when the page is fully loaded, I hide the loading div. My php script goes to amazon api, ebay api and screen dumps several other sites, and instead of a static loading div, I would like to update this loading div with the following
Downloading amazon.com
// amazon progress when finished updating loading div
Downloading ebay.com
etc .. and others.
Can this be done? if so what's the best approach? Thanks in advance for your help!
source to share
Use AJAX to ask PHP what it is doing at the moment and have a queue system that determines which tasks are completed or not. Here's a very simple example:
When the task is running, set the following variables:
$_SESSION['isEbayLoaded'] = false;
$_SESSION['isAmazonLoaded'] = false;
Once PHP loads eBay, it will set isEbayLoaded to true, and then AJAX can just ask PHP if ebay was loaded, then amazon and then whatever you need. All PHP will have to check if these variables were set as true, which would mean that the tasks were completed.
source to share
I've done this before with a very simple solution:
ob_flush();
This required creating a single div with the 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 will use the background style to show which site you will be scraping on.
If you must use markup, you can simply print a new "Loading" div each time (with a unique ID) along with the style to set the previous div style to "display: none".
Of course, remember that some browsers need at least 1024 bytes (something 4k!) Before doing anything.
source to share
I would make a request for each site and use the output from that page (XML, JSON, whatever) to let another page know what is going on.
For example, psuedo code:
- collect sites
- for the loop that loads the ajax page
- The ajax page returns every request with a status message eg. - "Loading Amazon ...."
What is it!
source to share