小編給大家分享一下基于thinkPHP框架如何實現留言板,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
代碼就實現了如下功能
1.留言功能。
2.驗證功能。
3.分頁顯示功能。
就是寫了幾行代碼(PS:頁面設計代碼不算,就算控制器和模型的代碼)
下面我公布一下控制的器的代碼,關于THINKPHP的代碼規則我就不闡述了,看thinkphp手冊就可以了。
class IndexAction extends Action
{
public function index() {
$Form = M("word");
// 按照id排序顯示前6條記錄
import("@.ORG.Page"); //導入分頁類
$count = $Form->count(); //計算總數
$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(); //模板調用,這個是關鍵。
}
//數據插入
public function insert() {
$word = D("word");
if($vo = $word->create())
{
if(false !== $word->add())
{
$this->success("數據添加成功");
}
else
{
$this->error('數據寫入錯誤!');
}
}
else
{
$this->error($word->getError());
}
}
//驗證重復
public function checkTitle()
{
if (!empty($_POST['username'])) {
$Form = M("word");
//getByTitle是model的獲取數據根據某字段獲取記錄的魔術方法
//比如getById etc getByXXX XXX大寫
if ($Form->getByUsername($_POST['username'])) {
$this->error('<font color=red>標題已經存在</font>');
} else {
$this->success('標題可以使用!');
}
} else {
$this->error('標題必須');
}
}
}下面是驗證模型的代碼
class wordModel extends Model{
protected $_validate = array(
array('username', 'require', '稱呼必須!', 1),//1為必須驗證
array('email', 'email', '郵箱格式錯誤!', 2),//2為不為空時驗證
array('qq','number','QQ號錯誤',2),
array('content', 'require', '內容必須',1),
array('username','','稱呼已經存在',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有一個要注意的,在CURD操作中,都規定要用表名。
以上是“基于thinkPHP框架如何實現留言板”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。