IT学习者 | 文章大全 | 技术文档 | 桌面壁纸 | 实用查询 | 网络电台 | 成语 | 歇后语 | 网址 | 下载 | 周公解梦 | 生日密码 | 电视剧365 | Flash
 您现在的位置: IT学习者 >> 文章大全 >> 网络编程 >> PHP

Smarty程序应用范例:留言簿(Guestbook)

【 作者:Surran    来源:网络  更新时间:2006-12-5 | 字体:

Smarty程序应用范例:留言簿(Guestbook)第四节

/web/www.example.com/smarty/guestbook/libs/guestbook.lib.php

<?php

/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt <monte [AT] ohrt [DOT] com>
* 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');        

    }
}

?>

guestbook.lib.php 是我们这个程序的应用类。它包含了整个程序的实现逻辑。让我们看看每一个方法。

类方法: Guestbook()
/**
* class constructor
*/
function Guestbook() {

// instantiate the sql object
$this->sql =& new GuestBook_SQL;
// instantiate the template object
$this->tpl =& new Guestbook_Smarty;
}

构造函数。每次我们使用这个留言簿对象时都执行。它把SQL语句和Smarty对象作为自己的属性,我们以后可以通过这个对象的方法来访问和使用它们(SQL语句和Smarty对象)。

类方法: displayForm()
/**
* 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');

}

displayForm() 该方法用于显示留言书写表单。它指派了模板文件中留言书写表单的变量和验证表单时的出错提示,然后把这个表单显示出来。

类方法: mungeFormData()
/**
* 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']);

}

mungeFormData() 该方法删掉来自表单输入内容开头和结尾的空白部分。这个方法在验证表单输入内容时最先调用。注意,表单信息是通过引用的办法传入本方法的,所以任何改变都会导致原始的数组内容(表单内容)发生改变。

类方法: isValidForm()
/**
* 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;
}

isValidForm() 该方法验证表单的输入。这里仅仅简单地验证表单的‘Name’和‘Comment’控件是否为空。如果是空的,对应的出错代码将被指派为本类错误属性的值。(这些错误代码接下来将被模板文件使用用以显示对应的错误提示。)


类方法: addEntry()
/**
* 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);

}

addEntry 该方法将向数据库中插入一条新的留言簿条目。注意,插入数据库的值已经进行必要操作,以避免SQL语法冲突和注入攻击。

类方法: getEntries()
/**
* get the guestbook entries
*/
function getEntries() {

$this->sql->query(
"select * from GUESTBOOK order by EntryDate",
SQL_ALL,
SQL_ASSOC
);

return $this->sql->record;
}

getEntries() 该方法将以“field => value”(效果同使用SQL_ASSOC参数)的格式读出数据库中所有的留言簿条目。

类方法: displayBook()
/**
* display the guestbook
*
* @param array $data the guestbook data
*/
function displayBook($data = array()) {

$this->tpl->assign('data', $data);
$this->tpl->display('guestbook.tpl');

}

displayBook() 该方法将显示出留言簿的条目。数组$data即存储留言簿条目的数组,将用来指派给模板文件并在模板文件中显示出来。

文章共5页,当前在第4页 9 7 [1] [2] [3] [4] [5] 8 :
相 关 文 章
相 关 软 件
没有相关下载
逃生 放生 黄玫瑰 想太多 那滋味 擦肩而过 放手去爱 北京欢迎你 依然在一起 吻得太逼真 感动天感动地 坐上火车去拉萨 怎么会狠心伤害我
心碎 冲动 小太阳 别碰我 蒲公英 千山万水 改变自己 一定要爱你 等爱的玫瑰 陷入爱里面 北极星的眼泪 最后一次的温柔 亲爱的那不是爱情
光荣 火花 坏女人 日不落 樱花草 为你写诗 独家记忆 夏天的味道 寂寞才说爱 忘不掉的伤 爱上你是个错 第三者的第三者 地球人都知道我爱你
假如 相思 是非题 有缘人 舍不得 我的答铃 死而无憾 外滩十八号 越爱越难过 123木头人 和寂寞说分手 爱上你是我的错 爱情里没有谁对谁错
加入收藏留言建议自助友情链接普通友情链接站长的Blog
版权所有   COPYRIGHT 2002-2008 ★IT学习者★ ALL RIGHTS RESERVED.