A complete working PHP guestbook application built with Tplix. Covers MVC structure, form validation, PDO database integration, and clean separation of presentation from business logic.
Sample Application
Guestbook
A complete working PHP + Tplix application demonstrating form processing, database integration, and clean presentation separation. Once you understand these concepts you can apply them to any programming pattern.
PHP + TplixMySQL + PDOForm ValidationMVC Pattern
This guide assumes you're already familiar with Apache and MySQL. It is not a guide on how to set those up. You may use this code at your own discretion and risk. Adjust paths and credentials for your environment.
Project File Structure
We'll build on top of the installation guide setup. Here are all the files and directories for the guestbook application:
index.phpApplication controller — handles all browser requests and routes to the correct action
guestbook_setup.phpInitialisation — loads libraries and configures the Tplix template object
guestbook.lib.phpMain application class — database queries, form validation, and template rendering
guestbook.tplView template — displays the list of guestbook entries
guestbook_form.tplForm template — the add-entry form with validation error display
1
Application Controller
index.php is the single entry point for the application — the only file the browser accesses directly. It defines paths, loads the setup script, and routes requests to one of three actions: view, add, or submit.
/docs/guestbook/index.php
<?php/**
* Guestbook Sample Tplix Application
* File: index.php — application controller
*/// define application directoriesdefine('GUESTBOOK_DIR', '/web/www.example.com/tplix/guestbook/');
define('TPLIX_DIR', '/usr/local/lib/php/Tplix/');
// load the setup scriptinclude(GUESTBOOK_DIR . 'libs/guestbook_setup.php');
// create guestbook object$guestbook = newGuestbook;
// determine which action was requested (default: view)$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
switch($_action) {
case'add':
// show the blank add-entry form$guestbook->displayForm();
break;
case'submit':
// process form submission$guestbook->mungeFormData($_POST);
if($guestbook->isValidForm($_POST)) {
$guestbook->addEntry($_POST);
$guestbook->displayBook($guestbook->getEntries());
} else {
$guestbook->displayForm($_POST); // redisplay with errors
}
break;
case'view':
default:
// display all entries$guestbook->displayBook($guestbook->getEntries());
break;
}
?>
action=view
Display entries
Default — loads and shows all guestbook rows
action=add
Show form
Renders the blank entry form
action=submit
Process form
Validates, saves, or redisplays with errors
2
Setup & Configuration
guestbook_setup.php extends the Tplix class to create a pre-configured template object with all directory paths set automatically whenever it's instantiated.
Run the following SQL script to create the database and table. Adjust the GRANT line to match your own database user and password.
guestbook.sql
CREATE DATABASE GUESTBOOK;
USE GUESTBOOK;
CREATE TABLE GUESTBOOK (
id int(11) NOT NULL AUTO_INCREMENT,
Name varchar(255) NOT NULL DEFAULT'',
EntryDate datetimeNOT NULL DEFAULT'0000-00-00 00:00:00',
Comment textNOT NULL,
PRIMARY KEY (id),
KEY EntryDate (EntryDate)
) TYPE=MyISAM;
-- change user/password to match your environmentGRANT ALL ON GUESTBOOK.* TO guestbook@localhost IDENTIFIED BY'foobar';
4
Application Library
guestbook.lib.php contains the entire application class. Let's walk through each method.
__construct()
Runs once when the Guestbook object is instantiated — sets up DB and template connections
guestbook.tpl loops over the entries assigned by displayBook(). It formats the date, HTML-escapes names and comments, and cycles row background colors.
HTML-escapes output — prevents XSS and tag conflicts
|date_format
Formats the raw datetime into a human-readable string
{cycle}
Alternates row background colours automatically
guestbook_form.tpl handles the add-entry form. If re-shown after a validation error, it repopulates the fields and displays the correct error message — all without any PHP in the template.
templates/guestbook_form.tpl
{* Tplix *}
<formaction="{$SCRIPT_NAME}?action=submit"method="post">
<tableborder="1">
{if$errorne""}
<tr>
<tdbgcolor="yellow"colspan="2">
{if$erroreq"name_empty"}
You must supply a name.
{elseif$erroreq"comment_empty"}
You must supply a comment.
{/if}
</td>
</tr>
{/if}
<tr>
<td>Name:</td>
<td>
<inputtype="text"name="Name"value="{$post.Name|escape}"size="40">
</td>
</tr>
<tr>
<tdvalign="top">Comment:</td>
<td>
<textareaname="Comment"cols="40"rows="10">
{$post.Comment|escape}
</textarea>
</td>
</tr>
<tr>
<tdcolspan="2"align="center">
<inputtype="submit"value="Submit">
</td>
</tr>
</table>
</form>
Notice: error messages are driven by error codes (name_empty, comment_empty) — not by PHP strings assigned from the application. The template owns the message wording entirely.
What this application demonstrates
All presentation is in the template
No HTML tags or markup are assigned from PHP. Only data values — names, dates, comments — are assigned. The template decides how to display them.
Error messages are template-owned
PHP assigns an error code. The template translates that code into a human-readable message — keeping wording changes entirely out of PHP.
PHP objects keep things clean
The PDO and Tplix objects are properties on the Guestbook class, available to all methods — no global variables or clunky parameter passing.
Ready to build your own?
Use this as a starting point and apply the same separation pattern to your own projects.