Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * MULTIPLE PROGRESS BARS DURING LONG PHP SCRIPT EXECUTION
- *
- * based on http://spidgorny.blogspot.com/2012/02/divcolored-for-lengthy-php-process.html
- * modified by ayukawaa@gmail.com (because I need it ^_^)
- * pure js, tested on IE 11, Firefox 27, Chrome 33
- * can be easely changed to jquery,...
- *
- * 2014-03-11
- */
- class ProgressBar
- {
- # default values
- var $set_time_limit = 0;// do we really need to touch this?
- var $uniqueid = '';// for multiple progress bars on same page
- var $width = '300px';
- var $percent = 0;
- var $decimals = 0;
- var $hide_when_done = false;
- var $txt_pre = '';// text to put before the caption (%)
- var $txt_post = '';// text to put after the caption (%)
- var $initial_display = false;//show text when rendering the progress but BEFORE starting process
- # default names of each <div =id"">
- var $divmain = 'divmain';
- var $divtext = 'divtext';
- var $divsubdiv = 'divsubdiv';
- var $divcolored = 'divcolored';
- var $divtransparent = 'divtransparent';
- # default css of each <div =id"">
- var $css_divmain = 'position:relative;';
- var $css_divtext = 'color:#000000;position:absolute;width:100%;height:16px;text-align:center;font-size:9px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;height: 16px;line-height: 16px;vertical-align: middle;';
- var $css_divsubdiv = 'width:100%;height:16px;border:1px solid #a9ddef;border-radius:5px 5px 5px 5px;';
- var $css_divcolored = 'float:left;height:16px;background-color:#43b6df;border-radius: 5px 5px 5px 5px;';
- var $css_divtransparent = 'float:left;border-radius: 0 5px 5px 0;';
- var $css_progressbar_finished = 'border-radius: 5px 5px 5px 5px !important;background-color:#e8e8e8 !important;';
- # js to do when progressbar starts/ends
- var $js_prehook = '';
- var $js_posthook = '';
- /**
- * class constructor
- * @param array $config array with the options we want overwritten
- * @return nothing OUTPUT TO SCREEN!
- */
- function __construct($config=null)
- {
- # save config array
- if(is_array($config))
- {
- foreach ($config as $key => $value)
- {
- $this->$key = $value;
- }
- }
- #1 parameter=uniqueid
- elseif(!is_null($config))
- {
- $this->uniqueid = $config;
- }
- # should be always 0?
- set_time_limit($this->set_time_limit);
- # write inmediatly all pending: <html><head></head><body>...
- flush();
- ob_flush();
- # get ready for progressbar action!
- ob_end_clean();
- ini_set('output_buffering', '0');
- }
- /**
- * draw on screen the progress bar without doing any command yet
- * @param string $width optional|width of the desired progress bar
- * @return nothing OUTPUT TO SCREEN!
- */
- public function render($width='')
- {
- if(!empty($width))
- {
- $this->width = $width;
- }
- //echo $GLOBALS['CONTENT'];$GLOBALS['CONTENT']='';
- echo $this->getContent();
- $this->flush();
- //$this->progress(0);
- }
- /**
- * do the REAL drawing on screen of the progress bar without doing any command yet
- * @return string the html+js+css needed
- */
- private function getContent()
- {
- $value = floatval($this->percent);
- $hide_when_done = $this->hide_when_done ? '1' : '0';
- $percent = ($this->initial_display) ? number_format($this->percent, $this->decimals, '.', '') .'%' : '';
- $content .= '
- <div id="PBCONTAINER'.$this->uniqueid.'">
- <div id="'.$this->divmain.$this->uniqueid.'">
- <div id="'.$this->divtext.$this->uniqueid.'">'.$percent.'</div>
- <div id="'.$this->divsubdiv.$this->uniqueid.'">
- <div id="'.$this->divcolored.$this->uniqueid.'" style="width:'.$value.'%;"></div>
- <div id="'.$this->divtransparent.$this->uniqueid.'"></div>
- </div>
- </div>
- </div>
- <style>
- #PBCONTAINER'.$this->uniqueid.'{width:'.$this->width.';}
- #'.$this->divmain.$this->uniqueid.'{'.$this->css_divmain.'}
- #'.$this->divtext.$this->uniqueid.'{'.$this->css_divtext.'}
- #'.$this->divsubdiv.$this->uniqueid.'{'.$this->css_divsubdiv.'}
- #'.$this->divcolored.$this->uniqueid.'{'.$this->css_divcolored.'}
- #'.$this->divtransparent.$this->uniqueid.'{'.$this->css_divtransparent.'}
- .progressbar_finished'.$this->uniqueid.'{'.$this->css_progressbar_finished.'}
- </style>
- <script>
- var jspbar_cache'.$this->uniqueid.'=false;
- var jspbar_txt_pre'.$this->uniqueid.'="'.$this->txt_pre.'";
- var jspbar_txt_post'.$this->uniqueid.'="'.$this->txt_post.'";
- var jspbar_hide_when_done'.$this->uniqueid.'='.$hide_when_done.';
- var jspbar_'.$this->divcolored.$this->uniqueid.'=document.getElementById("'.$this->divcolored.$this->uniqueid.'");
- var jspbar_'.$this->divmain.$this->uniqueid.'=document.getElementById("'.$this->divmain.$this->uniqueid.'");
- var jspbar_'.$this->divtransparent.$this->uniqueid.'=document.getElementById("'.$this->divtransparent.$this->uniqueid.'");
- var jspbar_'.$this->divtext.$this->uniqueid.'=document.getElementById("'.$this->divtext.$this->uniqueid.'");
- //cache dom
- function jspbar_cache_dom'.$this->uniqueid.'()
- {
- jspbar_'.$this->divcolored.$this->uniqueid.'=document.getElementById("'.$this->divcolored.$this->uniqueid.'");
- jspbar_'.$this->divmain.$this->uniqueid.'=document.getElementById("'.$this->divmain.$this->uniqueid.'");
- jspbar_'.$this->divtransparent.$this->uniqueid.'=document.getElementById("'.$this->divtransparent.$this->uniqueid.'");
- jspbar_'.$this->divtext.$this->uniqueid.'=document.getElementById("'.$this->divtext.$this->uniqueid.'");
- jspbar_cache'.$this->uniqueid.'=true;
- jspbar_prehook'.$this->uniqueid.'("'.$this->uniqueid.'");
- }
- //called each time
- function jspbar_run'.$this->uniqueid.'(progress, text, end)
- {
- if(!jspbar_cache'.$this->uniqueid.')
- {
- jspbar_cache_dom'.$this->uniqueid.'();
- }
- if(jspbar_'.$this->divcolored.$this->uniqueid.')
- {
- jspbar_'.$this->divcolored.$this->uniqueid.'.style.width=progress+"%";
- if(end==1)
- {
- if(jspbar_hide_when_done'.$this->uniqueid.'==1)
- {
- jspbar_'.$this->divmain.$this->uniqueid.'.style.display="none";
- }
- jspbar_'.$this->divcolored.$this->uniqueid.'.style.width="100%";//the end can be 100% or 0%
- jspbar_'.$this->divcolored.$this->uniqueid.'.className+=jspbar_'.$this->divcolored.$this->uniqueid.'.className ? "progressbar_finished'.$this->uniqueid.'" : " progressbar_finished'.$this->uniqueid.'";
- jspbar_'.$this->divtext.$this->uniqueid.'.innerHTML=text;//without PRE or POST!!!
- jspbar_posthook'.$this->uniqueid.'("'.$this->uniqueid.'");
- }
- else
- {
- var last_percent=100-progress;
- jspbar_'.$this->divtransparent.$this->uniqueid.'.style.width=last_percent+"%";
- if (text!="")
- {
- jspbar_'.$this->divtext.$this->uniqueid.'.innerHTML=jspbar_txt_pre'.$this->uniqueid.'+text+jspbar_txt_post'.$this->uniqueid.';
- }
- }
- }
- }
- //called before start
- function jspbar_prehook'.$this->uniqueid.'(uniqueid)
- {
- '.$this->js_prehook.'
- }
- //called when finished
- function jspbar_posthook'.$this->uniqueid.'(uniqueid)
- {
- '.$this->js_posthook.'
- }
- </script>';
- return $content.PHP_EOL;
- }
- /**
- * write and do the specific progress
- * @param string $percent percent done
- * @param string $text optional|string to display (values=null:write nothing, empty:write default %, else:write 'as is')
- * @param bool $end optional|end of progress bar reached (100% or 0%)
- */
- function progress($percent, $text = null, $end=null)
- {
- # only send END parameter when really ends (100% or 0%)
- $this->percent = number_format($percent, $this->decimals, '.', '');
- if(is_null($text))
- {
- $text_parameter = '';
- }
- elseif(empty($text))
- {
- $text_parameter = $this->percent.'%';
- }
- else
- {
- $text_parameter = $text;
- }
- $end_parameter = is_null($end) ? '' : ',1';
- echo '<script>jspbar_run'.$this->uniqueid.'("'.$percent.'","'.htmlspecialchars($text_parameter).'"'.$end_parameter.');</script>';
- $this->flush();
- }
- function flush()
- {
- print str_pad('', intval(ini_get('output_buffering'))).PHP_EOL;
- //ob_end_flush();
- flush();
- }
- }
- /* EOF */
- ?><!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta name="description" content="Multiple progress bars during long PHP script execution">
- <meta name="author" content="ayukawaa">
- <title>TEST • Multiple progress bars during long PHP script execution by ayukawaa</title>
- </head>
- <body><h1 id="title">Multiple progress bars during long PHP script execution</h1>
- <hr>
- <?php
- /**
- * MULTIPLE PROGRESS BARS DURING LONG PHP SCRIPT EXECUTION
- *
- * based on http://spidgorny.blogspot.com/2012/02/divcolored-for-lengthy-php-process.html
- * modified by ayukawaa@gmail.com (because I need it ^_^)
- * pure js, tested on IE 11, Firefox 27, Chrome 33
- * can be easely changed to jquery,...
- *
- * 2014-03-11
- */
- /*
- require_once('class.progressbar.php');
- */
- # --- PROGRESS BAR ---
- $config = array();
- $config['width'] = '600px';
- $config['uniqueid'] = '_0';
- $config['txt_pre'] = 'UN-DOING SOME NASTY ACTION: ';
- $config['txt_post'] = '... ha,ha,ha';
- $config['percent'] = 100;//initial
- $config['decimals'] = 2;
- $config['css_divmain'] = 'position:relative;';
- $config['css_divtext'] = 'position:absolute;width:400px;padding-top:6px;height:6px;text-align:left;font-size:9px;font-family: Verdana;';
- $config['css_divsubdiv'] = 'width:100%;height:4px;border:1px solid #a9ddef;';
- $config['css_divcolored'] = 'float:left;height:4px;background-color:#43b6df;';
- $config['css_divtransparent'] = 'float:left;';
- $config['css_progressbar_finished'] = 'background-color:#eeeeee !important;';
- $config['js_posthook'] = 'document.getElementById("dom_write").innerHTML="ProgressBar ["+uniqueid+"] finished!!!";';
- $pb0 = new ProgressBar($config);
- # --- PROGRESS BAR ---
- $config = array();
- $config['width'] = '600px';
- $config['uniqueid'] = '_9';
- $config['txt_pre'] = 'REAL PROCESS: ';
- $config['decimals'] = 0;
- $config['css_divmain'] = 'position:relative;';
- $config['css_divtext'] = 'color:#86610b;position:absolute;width:100%;height:40px;text-align:center;font-size:22px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;line-height: 40px;vertical-align: middle;';
- $config['css_divsubdiv'] = 'width:100%;height:40px;border:1px solid #86610b;';
- $config['css_divcolored'] = 'float:left;height:40px;background-color:#f5cb68;';
- $config['css_divtransparent'] = 'float:left;';
- $config['css_progressbar_finished'] = 'background-color:#86610b !important;';
- $config['js_prehook'] = 'document.getElementById("title").innerHTML="PRE HOOK: Starting 2nd process";';
- $config['js_posthook'] = 'alert("REAL PROCESS: ["+uniqueid+"] finished!!!");document.getElementById("title").innerHTML="Multiple progress bars during long PHP script execution";';
- $pb9 = new ProgressBar($config);
- # --- PROGRESS BAR ---
- $config = array();
- $config['width'] = '600px';
- $config['uniqueid'] = '_1';
- $config['txt_pre'] = 'Nested ';
- $config['txt_post'] = ' (+rounded borders)';
- $config['percent'] = 0;//initial
- $config['decimals'] = 1;
- $config['css_divmain'] = 'position:relative;';
- $config['css_divtext'] = 'color:#0002ff;position:absolute;width:100%;height:40px;text-align:center;font-size:18px;font-family: times, serif;line-height: 40px;vertical-align: middle;';
- $config['css_divsubdiv'] = 'width:100%;height:40px;border:1px solid #a9ddef;border-radius: 10px 10px 10px 10px;';
- $config['css_divcolored'] = 'float:left;height:40px;background-color:#43b6df;border-radius: 10px 10px 10px 10px;';
- $config['css_divtransparent'] = 'float:left;border-radius: 0 10px 10px 0;';
- $config['css_progressbar_finished'] = 'border-radius: 10px 10px 10px 10px !important;background-color:#43b6df !important;';
- $pb1 = new ProgressBar($config);
- # --- PROGRESS BAR ---
- $config = array();
- $config['uniqueid'] = '_2';
- $config['width'] = '400px';
- $config['txt_pre'] = 'Getting clients for emailing ';
- $config['txt_post'] = '...';
- $config['percent'] = 0;
- $config['decimals'] = 3;
- $config['hide_when_done'] = true;
- $config['css_divmain'] = 'position:relative;padding-left:50px;';
- $config['css_divtext'] = 'position:absolute;width:100%;height: 16px;text-align:center;font-size:9px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;line-height: 16px;vertical-align: middle;';
- $config['css_divsubdiv'] = 'width:100%;height:16px;border:1px solid #ffddef;border-radius:5px 5px 5px 5px;';
- $config['css_divcolored'] = 'float:left;height:16px;background-color:#ffb6df;border-radius: 5px 5px 5px 5px;';
- $config['css_divtransparent'] = 'float:left;border-radius: 0 5px 5px 0;';
- $config['css_progressbar_finished'] = 'border-radius: 5px 5px 5px 5px !important;background-color:#ffaaaa !important;';
- $pb2 = new ProgressBar($config);
- # --- PROGRESS BAR ---
- $config = array();
- $config['uniqueid'] = '_3';
- $config['width'] = '400px';
- $config['txt_pre'] = 'Sending email to clients ';
- $config['txt_post'] = '';
- $config['percent'] = 0;
- $config['decimals'] = 2;
- $config['hide_when_done'] = true;
- $config['css_divmain'] = 'position:relative;padding-left:100px;';
- $config['css_divtext'] = 'position:absolute;width:100%;height:10px;text-align:center;font-size:8px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;line-height:10px;vertical-align: middle;';
- $config['css_divsubdiv'] = 'width:100%;height:10px;border:1px solid #9acd32;';
- $config['css_divcolored'] = 'float:left;height:10px;background-color:#e0eebb;';
- $config['css_divtransparent'] = 'float:left;';
- $config['css_progressbar_finished'] = 'background-color:#ffaaaa !important;';
- $pb3 = new ProgressBar($config);
- # --- PROGRESS BAR ---
- $pb4 = new ProgressBar('_MyNewUniqueID');//all default
- # write headers and functions
- echo 'ALL OPTIONS BY DEFAULT:<br />';
- $pb4->render();
- echo '<br /><p>DOM element written at end: <strong><span id="dom_write">Waiting...</span></strong></p>';
- $pb0->render();
- echo '<br />';
- $pb9->render();
- echo '<br />';
- # start procesing
- $max_value_for_pbar_0 = 305;
- $max_value_for_pbar_1 = 7;
- $max_value_for_pbar_2 = 3;
- $max_value_for_pbar_3 = 53;
- $max_value_for_pbar_4 = 7000;
- # ----------------------------------
- # progressbar 4: ALL OPTIONS BY DEFAULT
- # ----------------------------------
- for ($x4 = 0; $x4 < $max_value_for_pbar_4; $x4++)
- {
- $percent_value_pbar_4 = $x4*100/$max_value_for_pbar_4;
- $text4 = number_format($percent_value_pbar_4, 2, '.', '').'%';
- $pb4->progress($percent_value_pbar_4, $text4);
- usleep(100);
- }
- $pb4->progress(100,null,true);
- # ----------------------------------
- # progressbar 0
- # ----------------------------------
- for ($x0 = $max_value_for_pbar_0; $x0 >0 ; $x0--)
- {
- $percent_value_pbar_0 = $x0*100/$max_value_for_pbar_0;
- $text0 = number_format($percent_value_pbar_0, 2, '.', '').'%';
- $pb0->progress($percent_value_pbar_0, $text0);
- usleep(10000);
- }
- $pb0->progress(0,null,true);//<- countdown to 0
- # ----------------------------------
- # progressbar 9
- # ----------------------------------
- $array = array('Starting job','Selecting clients','Reading accounts','Procesing orders','Taking the money ^_^','Sending emails','Deleting trash');
- $max_value_for_pbar_9 = count($array);
- for ($x9 =0;$x9 < $max_value_for_pbar_9; $x9++)
- {
- $percent_value_pbar_9 = $x9*100/$max_value_for_pbar_9;
- $text9 = $array[$x9];
- $pb9->progress($percent_value_pbar_9, $text9);
- usleep(1000000);
- }
- $pb9->progress(100,null,true);
- # ----------------------------------
- # progressbars 1-2-3 nested
- # ----------------------------------
- // draw progressbars now!
- $pb1->render();
- echo '<br />';
- $pb2->render();
- echo '<br />';
- $pb3->render('100%');//width !!!
- for ($x1 = 0; $x1 < $max_value_for_pbar_1; $x1++)
- {
- $percent_value_pbar_1 = $x1*100/$max_value_for_pbar_1;
- $text1 = number_format($percent_value_pbar_1, 2, '.', '').'%';
- $pb1->progress($percent_value_pbar_1, $text1);
- usleep(100000);
- for ($x2 = 0; $x2 < $max_value_for_pbar_2; $x2++)
- {
- $percent_value_pbar_2 = $x2*100/$max_value_for_pbar_2;
- $text2 = number_format($percent_value_pbar_2, 2, '.', '').'%';
- $pb2->progress($percent_value_pbar_2, $text2);
- usleep(100000);
- for ($x3 = 0; $x3 < $max_value_for_pbar_3; $x3++)
- {
- $percent_value_pbar_3 = $x3*100/$max_value_for_pbar_3;
- $text3 = number_format($percent_value_pbar_3, 2, '.', '').'%';
- $pb3->progress($percent_value_pbar_3, $text3);
- usleep(10000);
- }
- }
- }
- $pb3->progress(100,null,true);
- $pb2->progress(100,null,true);
- $pb1->progress(100,'All process done!',true);
- echo '<hr>Done.<br />';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement