《PHP實(shí)例:基于thinkPHP框架實(shí)現(xiàn)留言板的方法》要點(diǎn):
本文介紹了PHP實(shí)例:基于thinkPHP框架實(shí)現(xiàn)留言板的方法,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:thinkphp教程
本文實(shí)例講述了基于thinkPHP框架實(shí)現(xiàn)留言板的方法.分享給大家供大家參考,具體如下:PHP應(yīng)用
奮斗了一天,終于THINKPHP小鄧留言版的概念版出來了PHP應(yīng)用
其實(shí)真的THINKPHP開發(fā)速度很快,作為一個(gè)互聯(lián)網(wǎng)上“搬磚”的,從事這種 純碼農(nóng)的事也是無可厚非的.PHP應(yīng)用
代碼就實(shí)現(xiàn)了如下功能PHP應(yīng)用
1.留言功能.PHP應(yīng)用
2.驗(yàn)證功能.PHP應(yīng)用
3.分頁顯示功能.PHP應(yīng)用
就是寫了幾行代碼(PS:頁面設(shè)計(jì)代碼不算,就算控制器和模型的代碼)PHP應(yīng)用
下面我公布一下控制的器的代碼,關(guān)于THINKPHP的代碼規(guī)則我就不闡述了,看thinkphp手冊就可以了.PHP應(yīng)用
class IndexAction extends Action { public function index() { $Form = M("word"); // 按照id排序顯示前6條記錄 import("@.ORG.Page"); //導(dǎo)入分頁類 $count = $Form->count(); //計(jì)算總數(shù) $p = new Page ( $count, 1 ); $list=$Form->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll(); $page = $p->show (); $this->assign ( "page", $page ); $this->assign ( "list", $list ); $this->display(); //模板調(diào)用,這個(gè)是關(guān)鍵. } //數(shù)據(jù)插入 public function insert() { $word = D("word"); if($vo = $word->create()) { if(false !== $word->add()) { $this->success("數(shù)據(jù)添加成功"); } else { $this->error('數(shù)據(jù)寫入錯(cuò)誤!'); } } else { $this->error($word->getError()); } } //驗(yàn)證重復(fù) public function checkTitle() { if (!empty($_POST['username'])) { $Form = M("word"); //getByTitle是model的獲取數(shù)據(jù)根據(jù)某字段獲取記錄的魔術(shù)方法 //比如getById etc getByXXX XXX大寫 if ($Form->getByUsername($_POST['username'])) { $this->error('<font color=red>標(biāo)題已經(jīng)存在</font>'); } else { $this->success('標(biāo)題可以使用!'); } } else { $this->error('標(biāo)題必須'); } } }
下面是驗(yàn)證模型的代碼PHP應(yīng)用
class wordModel extends Model{ protected $_validate = array( array('username', 'require', '稱呼必須!', 1),//1為必須驗(yàn)證 array('email', 'email', '郵箱格式錯(cuò)誤!', 2),//2為不為空時(shí)驗(yàn)證 array('qq','number','QQ號錯(cuò)誤',2), array('content', 'require', '內(nèi)容必須',1), array('username','','稱呼已經(jīng)存在',0,'unique',1) ); protected $_auto = array( array('datetime', 'get_date',1, 'callback'), array('ip','getip',1,'callback') ); protected function get_date() { return date("Y-m-d H:i:s"); } protected function getip() { return $_SERVER['REMOTE_ADDR']; } }
thinkphp有一個(gè)要注意的,在CURD操作中,都規(guī)定要用表名.PHP應(yīng)用
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》.PHP應(yīng)用
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助.PHP應(yīng)用
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/3025.html