home forums resources search newsjoinmembers: 6057
Hiveminds Network: Joomla Wordpress Drupal Fireorb Flash Java PHP Ruby Windows Linux
Carl | Sat, 2007-09-01 07:48  tags: , ,

I needed a very simple progress indicator for a PHP migration script. Most of the ones I saw on the web were too involved. So I wrote a little snippet that writes to the page using javascript. Place the following PHP at the end of the while loop or whatever iterating process and the script will change the inner.HTML of a tag on the page. I used a dash so it grows from one character to four then resets. This provides the cute little animation that seems to hold every web developers attention for hours on end.

Of course I could make this more involved by adding in a bunch of changable variables and fancy characters. But I leave it up to those that like that kind of stuff to do it and post the changes here if they like.

<html><title></title><head></head>
<body>
<b id="indicator">-</b>processing

<?php

$count 
0;
for (
$i 1$i <= 10$i++) {
if (
$count == 0){
          
$a="-    ";
        }
        if (
$count == 1){
          
$a="--";
        }
        if (
$count == 2){
          
$a="---";
        }
        if (
$count == 3){
          
$a="----";
        }
         
$dot '<script type="text/javascript">document.getElementById(\'indicator\').innerHTML = \''.$a.'\';</script>' ;
           
//$dot .= "<div><span style=\"background:".$color.";height:20px;width:". (400 + ($count * 100)).";\"> $source_file </span>";
           //$dot .= "<span style=\"background:#e5e5e5;height:20px;width:". (400 + ($count * 100)).";\"> $destination_file </span></div>"; 
         
flush();
         echo 
$dot;
         
$dot '';
         
$count++;
         if(
$count == 4){
           
$count 0;
         }
    }
}
?>

</body></html>

The nice thing about doing a PHP progress indicator this way rather than with a animated gif is that you get an idea of how long the code takes to run through each loop. Since I am doing a migration of a previous website over to Drupal I want to be able to see if the loop hangs on a process or if a particular row of information takes more time to finish. I left in the div tag indicator code for those that want to have a colored progress idicator. Lastly, this is not a progress bar. The difference between the two being that a progress bar show the amount of work done and how much until completion. A progress indicator just shows that something is happening. Much like the indicator used in a web browser to show that a page is loading.

Carl's picture
With ten years of experience in web development he spends most of his work day developing in PHP,mySQL and Drupal
Carl McDade - Systems Developer
 
a Visitor posted on: Sat, 2007-09-01 11:08.

Your script is maybe appropriate for smaller loops, but if it gets to heavy operations, your script makes the common browser use 100% CPU, also, the flush() function is not usable in most applications especially when working with frameworks.
Use Ajax instead, thats the cleaner (and with available bibs. the easier solution)

Carl posted on: Sat, 2007-09-01 15:26.

I ran this script on some very heavy operations. This involved copying and cloning 70,000 images into 420,000 copies, 3500 users to a mysql database and watermarking all the images. There also were some minor operations involving converting the old propietory PHP site to Drupal.

The operation takes 9 hours and at no time did the web browser ,firefox 2.0, hang or complain. So the only thing I can say since it works perfectly for me is that "your milage may vary".

a Visitor posted on: Mon, 2008-03-24 13:57.

nice tutorial, I have been looking for something this simple, I had the same experience in that everything else was too complicated for my purposes.

Likewise I put this script through its paces using only the javascript segment in a while loop, no cpu issues at all with loops of 30,000 or more, the display updating 100s of times a second.

a Visitor posted on: Tue, 2008-05-13 16:51.

Dude.... you are sooo money!!!
This worked like a charm
Thanks

Bitrix Site manager - fast to create, easy to manage CMS Comparison Matrix
Put Your Site Here Developer Links
Wordpress Web Developers Web Developers Silverlight Drupal Silverlight Drupal Content Management Systems
 

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.