Examples TOCexamples TOC

Basic Observer

$Date: 2004/04/18 13:25:44 $

 Table of contents

Introduction

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'.

[Top]

 PHP script

Build the progress bar


<?php 
require_once 'HTML/Progress.php';

$bar = new HTML_Progress();
$bar->setAnimSpeed(100);
$bar->setIncrement(5);
?>

Create and attach the listener


<?php 
$observer = new MyObserver('screen');

$ok = $bar->addListener($observer);
if (!$ok) {
    die ("Cannot add a valid listener to progress bar !");
}
?>

[Top]

 Render options

Here are options to build the progress bar string :
color = red
HTML_Progress_UI::setStringAttributes()

[Top]

 Output

Screenshot

[Top]

 Play full example

Run the script below :


<?php 
require_once 'HTML/Progress.php';
require_once 'HTML/Progress/observer.php';

// 1. Defines ProgressBar observer
class MyObserver extends HTML_Progress_Observer
{
    var $_console;
    var $_out;
    
    function MyObserver($out)
    {
        $this->_console = '.' . DIRECTORY_SEPARATOR . 'observer_complex.log';
        $this->HTML_Progress_Observer();
        $this->_out = strtolower($out);
    }

    function notify($event)
    {
        if (is_array($event)) {
            $log = isset($event['log']) ? $event['log'] : "undefined event id.";
            $val = isset($event['value']) ? $event['value'] : "unknown value";
            $msg = "$log = $val";
        } else {
            $msg = $event;
        }
        if ($this->_out == 'file') {
            error_log("$msg \n", 3, $this->_console);
        } else {
            print ("$msg <br />\n");
    }
    }
}

// 2. Creates ProgressBar
$bar = new HTML_Progress();
$bar->setAnimSpeed(100);
$bar->setIncrement(5);

// 3. Creates and attach a listener 
$observer = new MyObserver('screen');
//$observer = new MyObserver('file');

$ok = $bar->addListener($observer);
if (!$ok) {
    die ("Cannot add a valid listener to progress bar !");
}

// 4. Changes look-and-feel of ProgressBar
$ui = $bar->getUI();
$ui->setStringAttributes('color = red');
$ui->setComment('Simple Observer ProgressBar example');
?>
<html>
<head>
<title>Simple Observer ProgressBar example</title>
<style type="text/css">
<!--
<?php echo $bar->getStyle(); ?>
// -->
</style>
<script type="text/javascript">
<!--
<?php echo $bar->getScript(); ?>
//-->
</script>
</head>
<body>

<?php 
echo $bar->toHTML(); 

do {
    $bar->display();
    if ($bar->getPercentComplete() == 1) {
        break;   // the progress bar has reached 100%
    }
    $bar->incValue();
} while(1);
?>

</body>
</html>
href:  ./observer_simple.php

[Top]