Ironetcart Андроид Ассемблер MASM32 Linux Все статьи Table of Contents


Javascript Progress Bar for PHP program

  Max Petrov June 2015

      There are long-playing PHP scripts. For example, the recalculation of forum statistics, and generally, any PHP function that requires an analysis of all messages, contained in the forum. With a large amount of calculations the PHP script will work during several minutes, tens of minutes, and we would like to have at least some means to control the progress of the execution to see that the PHP script does not hang up and to estimate the time left before its completion.

      The Internet proposes for this purpose some sophisticated solutions, like the following: to use sockets, ajax, jquery. But you can do it simply, and if it is possible to do it simply, then there's no need to complicate.

      Javascript will help us. Javascript is executed by the browser as soon as it is loaded. In other words, as soon as the browser receives the closing tag </script>, the browser starts to execute all code enclosed in the container <script type="text/javascript"> ... </script>. Therefore, to enforce the browser to reflect the progress of the program on the server (PHP-program), we just need to send from the server to the browser, by pieces, Javascript which illustrates the status of execution.

      Let's say we have a big cycle in PHP, in which some calculations are made:

<?php set_time_limit(600); progress( 'start', 0, 1 ); for ( $i=0; $i<100000; $i++ ) { // HERE ARE CALCULATIONS progress( 'run', ($a/100000)*100, 1 ); } progress( 'stop', 100, 1 ); set_time_limit(30); ?>

Progress(...) is a PHP function that will send Javascript pieces to the browser with the information about the progress of the cycle execution. The function is:

<?php function progress($status, $percent, $step) { global $percentlast; switch ($status) { case 'start': echo ' <script type="text/javascript"> document.getElementById("progress").style.background = "red"; document.getElementById("progress").style.height = "2px"; document.getElementById("progress").style.width = "' . $percent . '%"; </script> '; flush(); break; case 'run': if ( @$percentlast + $step <= $percent ) { $percentlast = $percent; echo ' <script type="text/javascript"> document.getElementById("progress").style.width = "' . $percent . '%"; </script> '; flush(); } break; case 'stop': echo ' <script type="text/javascript"> document.getElementById("progress").style.height = "0px"; document.getElementById("progress").style.width = "0%"; </script> '; flush(); break; } } ?>

      As you can see, the function progress sends to the output stream (sends to the browser) the trivial Javascript, the essence of which is the stretch of the empty (without text) HTML container <div id="progress"></div>, presented in the HTML page. The Javascript instructions sent by the server cause this container to extend in proportion to the degree of execution of the PHP script.

      The parameter $step is the step in percentages of execution between the server sendings of fragments of Javascript to the browser. For example, if $step = 1, the server will send data only if the percentage of PHP script execution is changed by at least 1 percent. At this value, from the server it will be sent totally no more than 102 Javascript fragments (including starting and terminal fragments). It can be said that the parameter $step saves traffic: the more it is, the less quantity of text will be sent to the browser, and the length change of <div id="progress"></div> with an increase of $step will become more abrupt.

      How it looks like. To the browser it is loaded the top part of the page up to the place at which the results of PHP calculations should be output. At the lower edge of the loaded part of the page appears the progress bar in the form of a strip, which, gradually increasing in length, moves to the right border of the page. The strip finally reaches the right border, after that it disappears, the results of calculations and all the content, which should be located below, are displayed.

Table of Contents

Assembler MASM32

      The simplest assembly program (beeper)
      Variables and Data Types of Assembler
      Registers of Processor IA32
      Numerical Systems, conversion of number
      Negative numbers

Other articles

      Frontend protection against Stored XSS Attacks (Javascript kills XSS)
      How to enforce Firefox to update favicon
      How to lock CD autorun in Windows XP
      Javascript Progress Bar for PHP program
      Schultz's tables (Shultzstables.exe)
      Speed of Forum Engine. Files or Database
      The best free programs for reading txt-files on Android smartphones
      Why a stupid forum needs a search option?


© Max Petrov При использовании материалов ссылка на обязательна