PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Tomas Lang   Stable   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example of using
Class: Stable
Compute statistics on a set of integer values
Author: By
Last change:
Date: 16 years ago
Size: 3,224 bytes
 

Contents

Class file image Download
<?php

   
require_once 'stable.php';

   
session_start();
   
    if ( !isset(
$_SESSION[ 'table' ] ) ) {
       
$_SESSION[ 'table' ] = new StatisticTable();
    }
   
    if ( isset(
$_POST[ 'compute' ] ) ) {
       
$data = explode( ',', $_POST[ 'values' ] );
       
$_SESSION[ 'table' ]->clear();
        foreach (
$data as $item ) {
           
$_SESSION[ 'table' ]->add( $item);
        }
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
        <title>Statistic table - test</title>
        <style type="text/css">
            table {
                border-left: 1px solid orange;
                border-top: 1px solid orange;
                border-collapse: collapse;
            }
            table th, table td {
                border-right: 1px solid orange;
                border-bottom: 1px solid orange;
            }

            th {
                background: orange;
            }

            td.center {
                text-align: center;
            }
           
            th.left {
                text-align: left;
            }

            table {
                background: yellow;
                margin: 1em 1em 0 0;
            }

            textarea {
                border: 1px solid orange;
                background: yellow;
            }

            input {
                background: orange;
                border: 3px solid red;
                font-size: 100%;
                font-weight: bold;
                margin-top: 0.5em;
            }
        </style>
    </head>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <form method="post">
            <textarea name="values" rows="10" cols="10" style="width: 100%"><?= $_POST[ 'values' ] ?></textarea>
            <input type="submit" name="compute" value="Compute" style="width: 100%" />
        </form>
        <table style="float: left;">
            <tbody>
                <?php $i = 0; $all = count( $_SESSION[ 'table' ]->getData() ); foreach ( $_SESSION[ 'table' ]->getSortedData() as $value => $count ): ?>
<tr>
                    <td class="center"><?= ++$i ?></td>
                    <td class="center"><?= $value ?></td>
                    <td class="center"><?= $count ?></td>
                    <td class="center"><?= round( ( $count / $all ) * 100, 4 ) ?></td>
                </tr>
                <?php endforeach; ?>
</tbody>
            <thead>
                <tr>
                    <th>i</th>
                    <th>x<sub>i</sub></th>
                    <th>n<sub>i</sub></th>
                    <th>%</th>
                </tr>
            </thead>
        </table>
        <table style="float: left;">
            <?php $info = $_SESSION[ 'table' ]->getTableInfo() ?>
<tr>
                <th class="left">Min</th>
                <td><?= round( $info[ 'min' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Max</th>
                <td><?= round( $info[ 'max' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Arithmetic mean</th>
                <td><?= round( $info[ 'arithmeticMean' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Quadratic mean</th>
                <td><?= round( $info[ 'quadraticMean' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Harmonic mean</th>
                <td><?= round( $info[ 'harmonicMean' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Median</th>
                <td><?= round( $info[ 'median' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Modus</th>
                <td><?= round( $info[ 'modus' ], 4 ) ?></td>
            </tr>
            <tr>
                <th class="left">Expansion</th>
                <td><?= round( $info[ 'expansion' ], 4 ) ?></td>
            </tr>
        </table>
    </html>
</html>