Examples TOCexamples

Simple ProgressBar Observer

$Date: 2005/07/25 12:27:28 $

 Table of contents

Introduction

This example requires :


This example will run a ProgressBar with a custom class observer mechanism. Output could be send to screen or on flat file 'observer_complex.log'.

Here are the contents of that file :

[Top]

 Render options

speed = 50 is set at line 46
HTML_Progress::setAnimSpeed()
increment = 5 is set at line 47
HTML_Progress::setIncrement()

[Top]

 Output

Screenshot

[Top]

 PHP source syntax highlight

  1. <?php
  2. /**
  3.  * Observer ProgressBar example. Uses a custom observer class.
  4.  *
  5.  * @version    $Id: simple.php,v 1.2 2005/07/25 12:27:28 farell Exp $
  6.  * @author     Laurent Laville <pear@laurent-laville.org>
  7.  * @package    HTML_Progress
  8.  * @subpackage Examples
  9.  */
  10.  
  11. require_once 'HTML/Progress.php';
  12. require_once 'HTML/Progress/observer.php';
  13.  
  14. // 1. Defines ProgressBar observer
  15. class MyObserver extends HTML_Progress_Observer
  16. {
  17.     var $_console;
  18.     var $_out;
  19.  
  20.     function MyObserver($out)
  21.     {
  22.         $this->_console = '.' . DIRECTORY_SEPARATOR . 'observer_complex.log';
  23.         $this->HTML_Progress_Observer();
  24.         $this->_out = strtolower($out);
  25.     }
  26.  
  27.     function notify($event)
  28.     {
  29.         if (is_array($event)) {
  30.             $log = isset($event['log']) ? $event['log'] : "undefined event id.";
  31.             $val = isset($event['value']) ? $event['value'] : "unknown value";
  32.             $msg = "$log = $val";
  33.         } else {
  34.             $msg = $event;
  35.         }
  36.         if ($this->_out == 'file') {
  37.             error_log("$msg \n", 3, $this->_console);
  38.         } else {
  39.             print ("$msg <br />\n");
  40.     }
  41.     }
  42. }
  43.  
  44. // 2. Creates ProgressBar
  45. $bar = new HTML_Progress();
  46. $bar->setAnimSpeed(50);
  47. $bar->setIncrement(5);
  48.  
  49. // 3. Creates and attach a listener
  50. $observer = new MyObserver($_GET['out']);
  51.  
  52. $ok = $bar->addListener($observer);
  53. if (!$ok) {
  54.     die ("Cannot add a valid listener to progress bar !");
  55. }
  56.  
  57. // 4. Changes look-and-feel of ProgressBar
  58. $ui = $bar->getUI();
  59. $ui->setComment('Simple Observer ProgressBar example');
  60. ?>
  61. <html>
  62. <head>
  63. <title>Simple Observer ProgressBar example</title>
  64. <style type="text/css">
  65. <!--
  66. <?php echo $bar->getStyle(); ?>
  67. // -->
  68. </style>
  69. <script type="text/javascript">
  70. <!--
  71. <?php echo $bar->getScript(); ?>
  72. //-->
  73. </script>
  74. </head>
  75. <body>
  76.  
  77. <?php
  78. echo $bar->toHtml();
  79. $bar->run();
  80. ?>
  81.  
  82. </body>
  83. </html>

[Top]