T086学习网 | 站长学院 | 技术文档 | 成语 | 歇后语 | 帝国时代 | 代码收藏 | IP地址查询 | 生活百科 | 生日密码 | CSS压缩 | 用户评论 | 欣欣百宝箱

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

【 网络作者:Surran 更新时间:2006-12-05 | 字体:
[导读]翻译:Surran pkstudio_comeback@yahoo.com转载请注明出处和译者原文见:http://smarty.php.net/sampleapp/sampleapp_p1.php Smarty程序应用范例:留言簿(Guestbook)第一节 这是一个使用了Smarty的PHP应用程序。目...

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即存储留言簿条目的数组,将用来指派给模板文件并在模板文件中显示出来。

上一页 [1] [2] [3] 4 [5] 下一页
  • 转载请注明来源:IT学习网 网址:http://www.t086.com/ 向您的朋友推荐此文章
  • 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系我们,我们会尽快予以更正。
更多
留言建议ASP探针PHP探针站长Enjoy的Blog
© 2017 T086学习网 - T086.com(原itlearner.com)
RunTime:19.56ms QueryTime:7