HomePHP Page 6 - Building A Quick-And-Dirty Guestbook With patGuestbook (part 1)
Code Poet - PHP
Want to plug into what your site visitors actually think? All you need is a little time, a dollop of imagination and a copy of patGuestbook. More, inside.
Now that the guestbook has been configured, all that's left is to integrate it into your Web site. And even that's a pretty simple task to accomplish - pop open the included sample PHP script, "example.php", which lives in your source distribution's root directory, and take a quick peek at what's inside it (I've added comments to the code so that it's easier to see what's happening):
<?php
// include required classes
require_once( "config/patGuestbook.php"
);
require_once( "include/patGuestbook.php" );
require_once( "include/patTemplate.php"
);
require_once( "include/patDbc.php" );
// initialize template engine
$template = new
patTemplate;
// set base directory to look for templates
$template->setBasedir(
$skins_dir );
// initialize DB abstraction layer
$dbc = new patMySqlDbc(
$db_host, $db_name,
$db_user, $db_pass );
// initialize guestbook
$guestbook = new
patGuestbook;
// connect guestbook to template engine and DB connection
$guestbook->setTemplate(
$template );
$guestbook->setDbc( $dbc );
// set skin to use for guestbook
if(
!empty( $skin ) )
$guestbook->setSkin( $skin );
// display guestbook
$guestbook->process(
array( "name" => "Voice Of The People" ) ); ?>
Nothing too complicated here. First, the script include()-s the files that contain
the API functions required by patGuestbook.
This is followed by initialization and creation of the patGuestbook object, which
serves as the focal point for all future guestbook operations, together with the template engine and database connection point.
// initialize template engine
$template = new patTemplate;
// initialize DB
abstraction layer
$dbc = new patMySqlDbc( $db_host, $db_name, $db_user,
$db_pass
);
// initialize guestbook
$guestbook = new patGuestbook;
// connect guestbook
to template engine and DB connection
$guestbook->setTemplate( $template ); $guestbook->setDbc(
$dbc );
Once that's done, the patGuestbook object's process() method can be used to select
and display the guestbook.
// display guestbook
$guestbook->process( array( "name" => "Voice Of The People"
) );
You can use the bare-bones skeleton above to create your own PHP scripts, or
even integrate the lines of code above into your existing site. Either way, this is all you need to do to get a guestbook up and running. Painless, isn't it?