Advertisement
Guest User

PHP Progress Bar during script execution

a guest
Feb 28th, 2012
16,680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Progress bar for a lengthy PHP process
  5.  * http://spidgorny.blogspot.com/2012/02/progress-bar-for-lengthy-php-process.html
  6.  */
  7.  
  8. class ProgressBar {
  9.     var $percentDone = 0;
  10.     var $pbid;
  11.     var $pbarid;
  12.     var $tbarid;
  13.     var $textid;
  14.     var $decimals = 1;
  15.  
  16.     function __construct($percentDone = 0) {
  17.         $this->pbid = 'pb';
  18.         $this->pbarid = 'progress-bar';
  19.         $this->tbarid = 'transparent-bar';
  20.         $this->textid = 'pb_text';
  21.         $this->percentDone = $percentDone;
  22.     }
  23.  
  24.     function render() {
  25.         //print ($GLOBALS['CONTENT']);
  26.         //$GLOBALS['CONTENT'] = '';
  27.         print($this->getContent());
  28.         $this->flush();
  29.         //$this->setProgressBarProgress(0);
  30.     }
  31.  
  32.     function getContent() {
  33.         $this->percentDone = floatval($this->percentDone);
  34.         $percentDone = number_format($this->percentDone, $this->decimals, '.', '') .'%';
  35.         $content .= '<div id="'.$this->pbid.'" class="pb_container">
  36.             <div id="'.$this->textid.'" class="'.$this->textid.'">'.$percentDone.'</div>
  37.             <div class="pb_bar">
  38.                 <div id="'.$this->pbarid.'" class="pb_before"
  39.                 style="width: '.$percentDone.';"></div>
  40.                 <div id="'.$this->tbarid.'" class="pb_after"></div>
  41.             </div>
  42.             <br style="height: 1px; font-size: 1px;"/>
  43.         </div>
  44.         <style>
  45.             .pb_container {
  46.                 position: relative;
  47.             }
  48.             .pb_bar {
  49.                 width: 100%;
  50.                 height: 1.3em;
  51.                 border: 1px solid silver;
  52.                 -moz-border-radius-topleft: 5px;
  53.                 -moz-border-radius-topright: 5px;
  54.                 -moz-border-radius-bottomleft: 5px;
  55.                 -moz-border-radius-bottomright: 5px;
  56.                 -webkit-border-top-left-radius: 5px;
  57.                 -webkit-border-top-right-radius: 5px;
  58.                 -webkit-border-bottom-left-radius: 5px;
  59.                 -webkit-border-bottom-right-radius: 5px;
  60.             }
  61.             .pb_before {
  62.                 float: left;
  63.                 height: 1.3em;
  64.                 background-color: #43b6df;
  65.                 -moz-border-radius-topleft: 5px;
  66.                 -moz-border-radius-bottomleft: 5px;
  67.                 -webkit-border-top-left-radius: 5px;
  68.                 -webkit-border-bottom-left-radius: 5px;
  69.             }
  70.             .pb_after {
  71.                 float: left;
  72.                 background-color: #FEFEFE;
  73.                 -moz-border-radius-topright: 5px;
  74.                 -moz-border-radius-bottomright: 5px;
  75.                 -webkit-border-top-right-radius: 5px;
  76.                 -webkit-border-bottom-right-radius: 5px;
  77.             }
  78.             .pb_text {
  79.                 padding-top: 0.1em;
  80.                 position: absolute;
  81.                 left: 48%;
  82.             }
  83.         </style>'."\r\n";
  84.         return $content;
  85.     }
  86.  
  87.     function setProgressBarProgress($percentDone, $text = '') {
  88.         $this->percentDone = $percentDone;
  89.         $text = $text ? $text : number_format($this->percentDone, $this->decimals, '.', '').'%';
  90.         print('
  91.         <script type="text/javascript">
  92.         if (document.getElementById("'.$this->pbarid.'")) {
  93.             document.getElementById("'.$this->pbarid.'").style.width = "'.$percentDone.'%";');
  94.         if ($percentDone == 100) {
  95.             print('document.getElementById("'.$this->pbid.'").style.display = "none";');
  96.         } else {
  97.             print('document.getElementById("'.$this->tbarid.'").style.width = "'.(100-$percentDone).'%";');
  98.         }
  99.         if ($text) {
  100.             print('document.getElementById("'.$this->textid.'").innerHTML = "'.htmlspecialchars($text).'";');
  101.         }
  102.         print('}</script>'."\n");
  103.         $this->flush();
  104.     }
  105.  
  106.     function flush() {
  107.         print str_pad('', intval(ini_get('output_buffering')))."\n";
  108.         //ob_end_flush();
  109.         flush();
  110.     }
  111.  
  112. }
  113.  
  114. echo 'Starting&hellip;<br />';
  115.  
  116. $p = new ProgressBar();
  117. echo '<div style="width: 300px;">';
  118. $p->render();
  119. echo '</div>';
  120. for ($i = 0; $i < ($size = 100); $i++) {
  121.     $p->setProgressBarProgress($i*100/$size);
  122.     usleep(1000000*0.1);
  123. }
  124. $p->setProgressBarProgress(100);
  125.  
  126. echo 'Done.<br />';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement