全部博文(921)
分类:
2006-11-05 22:02:41
php
/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt
* Date: March 14th, 2005
* File: index.php
* Version: 1.0
*/
// define our application directory
define('GUESTBOOK_DIR', '/web/');
// define smarty lib directory
define('SMARTY_DIR', '/usr/local/lib/php/Smarty/');
// include the setup script
include(GUESTBOOK_DIR . 'libs/guestbook_setup.php');
// create guestbook object
$guestbook =& new Guestbook;
// set the current action
$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
switch($_action) {
case 'add':
// adding a guestbook entry
$guestbook->displayForm();
break;
case 'submit':
// submitting a guestbook entry
$guestbook->mungeFormData($_POST);
if($guestbook->isValidForm($_POST)) {
$guestbook->addEntry($_POST);
$guestbook->displayBook($guestbook->getEntries());
} else {
$guestbook->displayForm($_POST);
}
break;
case 'view':
default:
// viewing the guestbook
$guestbook->displayBook($guestbook->getEntries());
break;
}
?>
php
/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt
* Date: March 14th, 2005
* File: guestbook_setup.php
* Version: 1.0
*/
require(GUESTBOOK_DIR . 'libs/sql.lib.php');
require(GUESTBOOK_DIR . 'libs/guestbook.lib.php');
require(SMARTY_DIR . 'Smarty.class.php');
require('DB.php'); // PEAR DB
// database configuration
class GuestBook_SQL extends SQL {
function GuestBook_SQL() {
// dbtype://user:pass@host/dbname
$dsn = "mysql://guestbook:foobar@localhost/GUESTBOOK";
$this->connect($dsn);
}
}
// smarty configuration
class Guestbook_Smarty extends Smarty {
function Guestbook_Smarty() {
$this->template_dir = GUESTBOOK_DIR . 'templates';
$this->compile_dir = GUESTBOOK_DIR . 'templates_c';
$this->config_dir = GUESTBOOK_DIR . 'configs';
$this->cache_dir = GUESTBOOK_DIR . 'cache';
}
}
?>
php
/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt
* Date: March 14th, 2005
* File: sql.lib.php
* Version: 1.0
*/
// define the query types
define('SQL_NONE', 1);
define('SQL_ALL', 2);
define('SQL_INIT', 3);
// define the query formats
define('SQL_ASSOC', 1);
define('SQL_INDEX', 2);
class SQL {
var $db = null;
var $result = null;
var $error = null;
var $record = null;
/**
* class constructor
*/
function SQL() { }
/**
* connect to the database
*
* @param string $dsn the data source name
*/
function connect($dsn) {
$this->db = DB::connect($dsn);
if(DB::isError($this->db)) {
$this->error = $this->db->getMessage();
return false;
}
return true;
}
/**
* disconnect from the database
*/
function disconnect() {
$this->db->disconnect();
}
/**
* query the database
*
* @param string $query the SQL query
* @param string $type the type of query
* @param string $format the query format
*/
function query($query, $type = SQL_NONE, $format = SQL_INDEX) {
$this->record = array();
$_data = array();
// determine fetch mode (index or associative)
$_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC : null;
$this->result = $this->db->query($query);
if (DB::isError($this->result)) {
$this->error = $this->result->getMessage();
return false;
}
switch ($type) {
case SQL_ALL:
// get all the records
while($_row = $this->result->fetchRow($_fetchmode)) {
$_data[] = $_row;
}
$this->result->free();
$this->record = $_data;
break;
case SQL_INIT:
// get the first record
$this->record = $this->result->fetchRow($_fetchmode);
break;
case SQL_NONE:
default:
// records will be looped over with next()
break;
}
return true;
}
/**
* connect to the database
*
* @param string $format the query format
*/
function next($format = SQL_INDEX) {
// fetch mode (index or associative)
$_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC : null;
if ($this->record = $this->result->fetchRow($_fetchmode)) {
return true;
} else {
$this->result->free();
return false;
}
}
}
?>
php
/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt
* Date: March 14th, 2005
* File: guestbook.lib.php
* Version: 1.0
*/
/**
* guestbook application library
*
*/
class Guestbook {
// database object
var $sql = null;
// smarty template object
var $tpl = null;
// error messages
var $error = null;
/**
* class constructor
*/
function Guestbook() {
// instantiate the sql object
$this->sql =& new GuestBook_SQL;
// instantiate the template object
$this->tpl =& new Guestbook_Smarty;
}
/**
* display the guestbook entry form
*
* @param array $formvars the form variables
*/
function displayForm($formvars = array()) {
// assign the form vars
$this->tpl->assign('post',$formvars);
// assign error message
$this->tpl->assign('error', $this->error);
$this->tpl->display('guestbook_form.tpl');
}
/**
* fix up form data if necessary
*
* @param array $formvars the form variables
*/
function mungeFormData(&$formvars) {
// trim off excess whitespace
$formvars['Name'] = trim($formvars['Name']);
$formvars['Comment'] = trim($formvars['Comment']);
}
/**
* test if form information is valid
*
* @param array $formvars the form variables
*/
function isValidForm($formvars) {
// reset error message
$this->error = null;
// test if "Name" is empty
if(strlen($formvars['Name']) == 0) {
$this->error = 'name_empty';
return false;
}
// test if "Comment" is empty
if(strlen($formvars['Comment']) == 0) {
$this->error = 'comment_empty';
return false;
}
// form passed validation
return true;
}
/**
* add a new guestbook entry
*
* @param array $formvars the form variables
*/
function addEntry($formvars) {
$_query = sprintf(
"insert into GUESTBOOK values(0,'%s',NOW(),'%s')",
mysql_escape_string($formvars['Name']),
mysql_escape_string($formvars['Comment'])
);
return $this->sql->query($_query);
}
/**
* get the guestbook entries
*/
function getEntries() {
$this->sql->query(
"select * from GUESTBOOK order by EntryDate DESC",
SQL_ALL,
SQL_ASSOC
);
return $this->sql->record;
}
/**
* display the guestbook
*
* @param array $data the guestbook data
*/
function displayBook($data = array()) {
$this->tpl->assign('data', $data);
$this->tpl->display('guestbook.tpl');
}
}
?>
{* Smarty *}
<table border="0" width="300">
<tr>
<th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">adda>)th>
tr>
{foreach from=$data item="entry"}
<tr bgcolor="{cycle values="#dedede,#eeeeee" advance=false}">
<td>{$entry.Name|escape}td>
<td align="right">{$entry.EntryDate|date_format:"%e %b, %Y %H:%M:%S"}td>
tr>
<tr>
<td colspan="2" bgcolor="{cycle values="#dedede,#eeeeee"}">{$entry.Comment|escape}
tr>
{foreachelse}
<tr>
<td colspan="2">No recordstd>
tr>
{/foreach}
table>
{* Smarty *}
<form action="{$SCRIPT_NAME}?action=submit" method="post">
<table border="1">
{if $error ne ""}
<tr>
<td bgcolor="yellow" colspan="2">
{if $error eq "name_empty"}You must supply a name.
{elseif $error eq "comment_empty"} You must supply a comment.
{/if}
td>
tr>
{/if}
<tr>
<td>Name:td>
<td><input type="text" name="Name" value="{$post.Name|escape}" size="40">td>
tr>
<tr>
<td valign="top">Comment:td>
<td><textarea name="Comment" cols="40" rows="10">{$post.Comment|escape}textarea>td>
tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit">td>
tr>
table>
form>