Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
BUSINESS LOGIC
PHP and MySQL example application - adhoc database analysis example from a Well House Consultants training course
More on PHP and MySQL example application - adhoc database analysis [link]
Source code: businesslogic.inc Module: H204
<?php

/* These are the things we need

read_config($from);

makeform_login();
$role = validate_login();
store_login();

makeform_basequery();
$tuneit = validate_base();
store_base();

makeform_rows();
validate_rows();
store_rows();

makeform_cols();
validate_cols();
store_cols();

makeform_grouping();
validate_groups();
store_groups();

makeform_result()
$whatnext = validate_results();
store_results();
run_query();

//////////////////////////////////////////////////////////
*/


require_once ("$include_from/sqlwrapper.inc");

function read_config($source) {
        $fh = fopen($source,"r");
        $_SESSION[config] = array();
        while ($line = fgets($fh,65536)) {
                $line = trim($line);
                if ($line == "" or ereg("^#",$line)) continue;
                $fields = explode(" ",$line);
                $fname = array_shift($fields);

                if (! is_array($_SESSION[config][$fname])) {
                        $_SESSION[config][$fname] = array();
                        }
                array_push($_SESSION[config][$fname],implode(" ",$fields));

                }
        }

function makeform_login() {
        $fields = array (
                array("name","Your name:",$_POST[name]),
                array("password","Your password:",$_POST[password],
                        "type" => "password"));
        $form = makeform($fields);
        return $form;
        }

function validate_login() {
        global $error;
        global $_warnings;
        $role = "";
        if ($_POST[name] != "demo") {
                $error = 1;
                $_warnings[name] = "Invalid account";
                $_warnings[password] = "Invalid account";
                }
        if ($_POST[password] != "xxx111") {
                $error = 1;
                $_warnings[name] = "Invalid account";
                $_warnings[password] = "Invalid account";
                }
        if ($error != 1) {
                $role = "user";
                }
        return $role;
        }

function store_login() {
        $_SESSION[user] = $_POST[name];
        }

function makeform_basequery() { // To be "Choose a standard query"
        $fields = array(array("bq","Which query?",$_POST[onto],
                        type => "submit",
                        options => "default|tuned"));
        $form = makeform($fields);
        return $form;
        }

function validate_base() {
        $_SESSION[wheres] = array();
        if ($_POST[bq] == "tuned") return 1;
        makeform_cols(); // To force column selection defaults
        // More to be added
        return 0; // tune it? Not at first!
        }

function store_base() {
        }

function makeform_rows() {
        $fields = array();
        foreach ($_SESSION[config]["whereopt:"] as $orow) {
                $testtypes = explode(" ",$orow);
                $testfield = array_shift($testtypes);
                foreach ($testtypes as $thistest) {
                        $rowtest = array("$testfield/$thistest",
                                "A <b>$thistest</b> test is on <b>$testfield</b>",
                                $_SESSION["$testfield/$thistest"]);
                        array_push($fields,$rowtest);
                        }
                }
        $form = makeform($fields);
        return $form;
        }

function makeform_cols() {
        $fields = array();
        foreach ($_SESSION[config]["displayopt:"] as $ocol) {
                $colinfo = explode(" ",$ocol);
                $testfield = array_shift($colinfo);
                $defcolstat = array_shift($colinfo);
                // Add handling to offer sort options
                if ($defcolstat != "always" and $defcolstat != "never") {
                if ($defcolstat == "on" and
                        ! is_string($_SESSION["$testfield/display"]))
                                $_SESSION["$testfield/display"] = "on";
                $coltest = array("$testfield/display",
                                "Display column <b>$testfield</b>",
                                $_SESSION["$testfield/display"],
                                type => "checkbox");
                array_push($fields,$coltest);
                }
                }
        $form = makeform($fields);
        return $form;
        }

function validate_rows() {
        // To add - check that lessthan contains a number and a host of
        // other checks
        }

function validate_cols() {
        // To add - checks
        }

function store_rows() {
        foreach ($_SESSION[config]["whereopt:"] as $orow) {
                $testtypes = explode(" ",$orow);
                $testfield = array_shift($testtypes);
                foreach ($testtypes as $thistest) {
                        $_SESSION["$testfield/$thistest"] =
                                stripslashes($_POST["$testfield/$thistest"]) ;
                }
        }
}

function store_cols() {
        foreach ($_SESSION[config]["displayopt:"] as $ocol) {
                $testtypes = explode(" ",$ocol);
                $discol = array_shift($testtypes);
                $disdef = array_shift($testtypes);
                $_SESSION["$discol/display"] =
                        stripslashes($_POST["$discol/display"]) ;
        }
}

function makeform_result() { // To be "what is your next action"
        $fields = array(array("onto","What next?",$_POST[onto],
                        type => "submit",
                        options => "new|logout|next|".
                                "previous|all|tailor|name"));

        $form = makeform($fields);
        return $form;
        }

function run_query() {

        $result = "";

// Get where clauses
        $wheres = array();
        foreach ($_SESSION[config]["whereopt:"] as $orow) {
                $testtypes = explode(" ",$orow);
                $testfield = array_shift($testtypes);
                foreach ($testtypes as $thistest) {
                        if ($_SESSION["$testfield/$thistest"]) {
                                switch ($thistest) {
case "is":
        $wcl = "$testfield = \"".
                        addslashes($_SESSION["$testfield/$thistest"]).
                        "\"";
        break;
case "contains":
        $wcl = "$testfield like \"%".
                        addslashes($_SESSION["$testfield/$thistest"]).
                        "%\"";
        break;
case "lessthan":
        $wcl = "$testfield < ".
                        addslashes($_SESSION["$testfield/$thistest"]);
        break;
case "greaterthan":
        $wcl = "$testfield > ".
                        addslashes($_SESSION["$testfield/$thistest"]);
        break;
default:
        $wcl = "1 = 1"; // We know we have a problem is we see this reported
        break;
}
        array_push($wheres,$wcl);
                }
        }
}
        $wherestring = implode(" and ",$wheres);
        if ($wherestring) $wherestring = " where $wherestring";

// Get Base query

        $basequery = $_SESSION[config]["basequery:"][0];

// Get Column list

        $whats = array();
        foreach ($_SESSION[config]["displayopt:"] as $ocol) {
                $testtypes = explode(" ",$ocol);
                $colwant = array_shift($testtypes);
                $defcol = array_shift($testtypes);
                if ($defcol == "never") continue;
                if ($defcol == "always") {
                        array_push($whats,$colwant);
                        continue;
                        }
                if ($_SESSION["$colwant/display"]) {
                        array_push($whats,$colwant);
                        }
        }

        $whatstring = implode(", ",$whats);
        if ($whatstring == "") $whatstring = "*";

// Make up complete query

        $query = "select $whatstring from $basequery $wherestring";

        sql_connect();
        $rset = sql_query($query);

        $result .= "Running <b>".htmlspecialchars($query)."</b><br><br>";
        $result .= "<table border=1>";
        while ($row = sql_fetch($rset)) {
                $rowcount++;
                if ($rowcount < 2) { // header row
                $result .= "<tr>";
                foreach (array_keys($row) as $col) {
                        $result .= cell($col,"head");
                        }
                $result .= "</tr>";
                }
                $result .= "<tr>";
                foreach (array_keys($row) as $col) {
                        $result .= cell($row[$col]);
                        }
                $result .= "</tr>";
                }
        $result .= "</table>";
        return $result;
}

function validate_results() {
        // What to do next?
        if ($_POST[onto] == "logout") return "logout";
        return "new";
        }

function store_results() {
        }

?>
Learn about this subject
This module and example are covered on the following public courses:
 * Learning to program in PHP
 * PHP Programming
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering PHP are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "PHP and MySQL example application - adhoc database analysis" training module. You'll find a description of the topic and some other closely related examples on the "PHP and MySQL example application - adhoc database analysis" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author
This web site is written and maintained by Well House Consultants.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb