從零構建具備發表、回覆、管理功能的留言板,採用 AJAX 無刷新提交,基於 Zend Framework MVC 架構,含數據庫設計、Model 封裝、Controller 邏輯與 View 模板。
表 message 新增字段:
pid:0=留言,非0=回覆對應的留言 ID。author、headimg、email、ip、show、addtime、updatetime。Message.phpclass Message extends Zend_Db_Table {
protected $_name = "message";
protected $_primary = 'id';
public function getAllMessage() { return $this->fetchAll("pid=0", "id DESC")->toArray(); }
public function getAllReMessage() { return $this->fetchAll("pid!=0", "id DESC")->toArray(); }
public function getMessageByID($id) { return $this->fetchAll('id='.$id)->toArray(); }
public function updateMessageByID($array, $id) { $this->update($array, $this->getAdapter()->quoteInto('id=?', $id)); }
public function delMessageByID($id) { $this->delete('id='.$id); return true; }
}
messageActionZend_Filter_StripTags 過濾輸入 → Zend_Validate_EmailAddress 驗證郵箱 → 組裝數據入庫 → 重新獲取列表渲染 message.phtml 片段(AJAX 回傳)。index.phtml 完整頁面。flag 區分用戶名為空(1)、回覆ID為空(2)、內容為空(3)、郵箱格式錯誤(5)、成功(0)。jQuery 提交表單 → 解析返回片段 → 追加至列表 → 清空輸入框。具體 JS 代碼見源碼下載。
header.phtml / footer.phtml:公共頭尾。message/index.phtml:列表容器。message/message.phtml:單條留言/回覆片段(AJAX 返還)。message/edit.phtml:編輯表單(預留)。完整實例代碼可點擊下載。更多 Zend 相關技術請參考專題:《Zend Framework 入門教程》、《PHP 優秀開發框架總結》、《Yii 框架入門及常用技巧總結》、《ThinkPHP 入門教程》、《PHP 面向對象程序設計入門教程》、《PHP+MySQL 數據庫操作入門教程》及《PHP 常見數據庫操作技巧匯總》。