Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Keeping the PHP and the HTML apart

Your Web application includes both presentation and logic, and that logic is further divided into business and display logic. How are you going to manage these various layers? How about keeping each of the logically separate areas in a separate file so that the look and feel can be maintained quite separate from the operations?

Here's a sample application written using this form of separation:

INDEX.PHP4 - THE TOP LAYER

<?
# Separating display, business logic and helpers
include "business.inc";
include "helpers.inc";
include "display.inc";
?>

DISPLAY.INC - THE PRESENTATION LAYER

<html>
<head><title><? title(); ?></title></head>
<body bgcolor=#CCCCFF><h1>Separating the logic</h1>
This is a demonstration of the separation of the "business logic"
from the main code on a PHP page. It's all transparent to the visitor
to your web site, but the page at the actual location called up is simply
a few includes and the real work is is in those:<ul><li>Business Logic
<li>Display Helper logic<li>Display template</ul>
<center><i>Using PHP as it's really intended ;-)</i></center><br><br>

<? result() ; ?>
<br><br>
<? question() ; ?> ...<p>
<form><table>
<tr><td>Let's have you enter an age:</td><td><? input_box("age",4); ?></td></tr>
<tr><td>and a gender</td><td><? input_radio("gender","Male|Female"); ?></td></tr>
<tr><td> and ... </td><td><input type=submit></td></tr>
</table>
</form><br><br>
<? copyright() ; ?>
</body>
</html>

HELPERS.INC - THE HELPERS LAYER

<?
function title() {
        global $title;
        echo $title;
        }
function question() {
        global $formheader;
        echo $formheader;
        }
function copyright() {
        echo "Copyright, Well House Consultants, ".date("Y");
        }
function result () {
        global $result;
        echo "** <u><b>$result</b></u> ** ";
        }
function input_radio($name,$options) {
        foreach (explode("|",$options) as $option) {
                echo "<input type=radio name=$name value=$option> $option<br>";
                }
        }
function input_box($name, $size) {
        echo "<input name=$name size=$size>";
        }
?>

BUSINESS.INC - THE BUSINESS LOGIC LEVEL

<?
$resolved = ($_GET[age] != "");

if ($_GET[age] > 19) {
        $describe = "Adult";
        if ($_GET[gender] == "Male") $describe = "Gentleman";
        if ($_GET[gender] == "Female") $describe = "Lady";
} elseif ($_GET[age] > 12) {
        $describe = "Teenager";
        if ($_GET[gender] == "Male") $describe = "Young Gentleman";
        if ($_GET[gender] == "Female") $describe = "Young Lady";
} elseif ($_GET[age] > 1) {
        $describe = "Child";
        if ($_GET[gender] == "Male") $describe = "Boy";
        if ($_GET[gender] == "Female") $describe = "Girl";
} else {
        $describe = "Baby";
}

if ($resolved) {
        $title = "About our person ....";
        $formheader = "Please enter details for another person";
        $result = "A $_GET[age] year old $_GET[gender] would be described as a '$describe'";
} else {
        $title = "A Welcome page";
        $formheader = "Please enter some information about a person";
        $result = "Your results will appear here";
        }

?>


See also PHP Best Practice - Module

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site
Related Material

Designing PHP-Based Solutions: Best Practice
Web site techniques, utility and visibility
resource index - PHP
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.


See also:

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