PHP Classes

File: DbProxyDemo.php

Recommend this page to a friend!
  Packages of Claudius Tiberiu Iacob   DbProxy   DbProxyDemo.php   Download  
File: DbProxyDemo.php
Role: Example script
Content type: text/plain
Description: See "HowToInstall.txt" for info
Class: DbProxy
Execute MySQL queries from SQL defined in XML
Author: By
Last change:
Date: 16 years ago
Size: 3,900 bytes
 

 

Contents

Class file image Download
<?php

    require_once ('domit/xml_domit_include.php');
    require_once ('ParamsProxy.php');
    require_once ('UTF8.php');
    require_once ('DbProxy.php');

    /**
    * Showcases the functionality of the DbProxy class.
    * The DbProxy class is an XML based SQL query aggregator and encoder, in one go. You place an 
    * SQL template inside an external XML file and transparently query the database by invoking 
    * its associated PHP method.
    * @author Claudius Tiberiu Iacob <claudius.iacob@gmail.com>.
    * @license Creative Commons Attribution Share Alike - Claudius Tiberiu Iacob 2009
    */
    class DbProxyDemo {

        private $dbProxy;

        public function __construct () {
            // Instantiate the proxy:
            $this->dbProxy = new DbProxy();
            // Hook up this class to the proxy:
            $this->dbProxy->configure($this);
            // Query the database:
            $this->dbProxy->createDemoTable ('M', 'F');
            $pageAction = trim ($_POST['action']);
            if ($pageAction == 'Add Employee') {
                $first = trim ($_POST['first_name']);
                $middle = trim ($_POST['middle_name']);
                $last = trim ($_POST['last_name']);
                $age = intval (trim ($_POST['age']));
                $sex = trim ($_POST['sex']);
                $job = trim ($_POST['job']);
                $salary = intval (trim ($_POST['salary']));
                $this->dbProxy->addEmployee ($first, $middle, $last, $age, $sex, $job, $salary);
                header ('Location: ' . basename (__FILE__)); // --> This clears POST data.
                exit();                                           //
            }
            $data = $this->dbProxy->getEmployees();
            // Print the page to the browser:
            $this->makePageHeader ();
            $this->makeTable ($data);
            $this->makeForm();
        }

        public function __destruct () {
            $this->makePageFooter ();
        }

        private function makePageHeader () {
            echo '<?xml version="1.0" encoding="UTF-8"?>
            <!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>
                <title>DbProxyDemo</title>
              </head>
                  <body>
                      <h2>Employees Table</h2>';
        }

        private function makePageFooter () {
            echo '
            </body>
            </html>';
        }

        private function makeTable ($tableData) {
            $ret = '
                <table border="1">
                    <tbody>
                        <tr>
                            <th>uid</th>
                            <th>First Name</th>
                            <th>Middle Name</th>
                            <th>Last Name</th>
                            <th>Age</th>
                            <th>Sex</th>
                            <th>Job</th>
                            <th>Salary</th>
                        </tr>';
            if (!empty ($tableData)) {
                foreach ($tableData as $tableRow) {
                    $ret .= '
                        <tr>';
                    foreach ($tableRow as $tableCell) {
                        $ret .= '
                            <td>' . $tableCell . '</td>';
                    }
                    $ret .= '
                        </tr>';
                }
            }
            $ret .= '
                    </tbody>
                </table>
            ';
            echo $ret;
        }

        private function makeForm () {
            echo '
                <form action="" method="post">
                    <h2>Registration Form</h2>
                    <fieldset>
                        <legend>Name</legend>
                        <p>
                            <label>
                                First: <input type="text" name="first_name" />
                            </label>
                            <label>
                                Middle: <input type="text" name="middle_name" />
                            </label>
                            <label>
                                Last: <input type="text" name="last_name" />
                            </label>
                        </p>
                    </fieldset>
                    <fieldset>
                        <legend>Profile</legend>
                        <p>
                            <label>
                                Age: <input type="text" name="age" />
                            </label>
                            <label>
                                Sex:
                                <select name="sex">
                                    <option>M</option>
                                    <option>F</option>
                                </select>
                            </label>
                        </p>
                    </fieldset>
                    <fieldset>
                        <legend>Info</legend>
                        <p>
                            <label>
                                Job: <input type="text" name="job" />
                            </label>
                            <label>
                                Salary: <input type="text" name="salary" />
                            </label>
                        </p>
                    </fieldset>
                    <p>
                        <input type="submit" name="action" value="Add Employee" />
                        <input type="reset" value="Reset" />
                    </p>
                </form>
            ';
        }
    }
    $dbProxyDemo = new DbProxyDemo();
?>