您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在ThinkPHP中利用無限級分類實現一個留言與回復功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
數據表:
復制代碼 代碼如下:
-- ----------------------------
-- Table structure for `wb_guestbook`
-- ----------------------------
DROP TABLE IF EXISTS `wb_guestbook`;
CREATE TABLE `eway_guestbook` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(10) NOT NULL,
`email` varchar(50) NOT NULL,
`path` varchar(100) NOT NULL,
`username` varchar(30) NOT NULL,
`updatetime` int(10) NOT NULL,
`ip` varchar(15) NOT NULL,
`url` varchar(200) NOT NULL,
`inputtime` int(10) NOT NULL,
`content` text NOT NULL,
`verify` varchar(32) NOT NULL,
`isreply` tinyint(1) NOT NULL,
`status` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=42 DEFAULT CHARSET=utf8;
代碼:
復制代碼 代碼如下:
<?php
// +----------------------------------------------------------------------
// | WBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.w3note.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 網菠蘿果
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* @class 留言板控制器GuestbookAction.class.php
+------------------------------------------------------------------------------
*/
class GuestbookAction extends CommonAction {
public function index(){
$garr= D('Guestbook')->gclist("id,username,inputtime,pid,url,content,path,concat(path,'-',id) as bpath");
$this->assign('Gklist', $garr['list']);
$this->assign('page',$garr['page']);
$this->display();
}
// +----------------------------------------------------------------------
// | 添加留言
// +----------------------------------------------------------------------
public function add(){
$this->adddata('Guestbook');
}
// +----------------------------------------------------------------------
// | 網址跳轉。如在表單url添加網址的話,點擊會跳轉到相關網站
// +----------------------------------------------------------------------
public function tourl(){
$this->gettourl('Guestbook');
}
}
?>
<?php
// +----------------------------------------------------------------------
// | WBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.w3note.com All rights reserved.
// | Author: 網菠蘿果
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* @function 留言板模型 類GuestbookModel.class.php
+------------------------------------------------------------------------------
*/
class GuestbookModel extends RelationModel{
// +----------------------------------------------------------------------
// | $_validate表單自動驗證
// +----------------------------------------------------------------------
protected $_validate = array(
array('email','require','請填寫您的郵箱!'),
array('email','email','郵箱格式錯誤!'),
);
// +----------------------------------------------------------------------
// | $_auto表單自動填充
// +----------------------------------------------------------------------
protected $_auto=array(
array('status','1'),
array('inputtime','time',1,'function'),
array('content','content',1,'callback'),
array('url','geturl',1,'callback'),
array ('inputtime','time',1,'function'),
array('path','path',3,'callback'),
array('username','getusername',3,'callback'),
);
// +----------------------------------------------------------------------
// | getusername()過濾用戶名
// +----------------------------------------------------------------------
public function getusername(){
if (isset ($_POST['username'])) {
if(trim($_POST['username'])=='網菠蘿果'){
return $data= ' ̄□ ̄';
}elseif(strlen($_POST['username']) >10){
return $data= msubstr($_POST['username'],0,5);
}else{
return $data= $_POST['username'];
}
}
}
// +----------------------------------------------------------------------
// | path()返回子類的path,父類的path的值為0
// +----------------------------------------------------------------------
public function path(){
$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;
$id=$_POST['id'];
if($pid==0){
return 0;
}
$fat=$this->where(array('id' => $pid))->find();
$data=$fat['path'].'-'.$fat['id'];
return $data;
}
// +----------------------------------------------------------------------
// | content()過濾留言內容
// +----------------------------------------------------------------------
public function content() {
if (isset ($_POST['content']) && !empty ($_POST['content'])) {
$data =deleteHtmlTags($_POST['content']);
$data =safeHtml($data);
if (strlen($data) > 1000) {
$data = msubstr($data, 0, 500);
}
return $data;
}
}
// +----------------------------------------------------------------------
// | content()過濾URL
// +----------------------------------------------------------------------
public function geturl(){
if (isset ($_POST['url'])) {
$data = deleteHtmlTags($_POST['url']);
$data = safeHtml($data);
return $data=$data?$data:"";
}
}
// +----------------------------------------------------------------------
// |gclist($field,$where='',$pagesize=30)留言列表
// +----------------------------------------------------------------------
// |$field,字段
// +----------------------------------------------------------------------
// |$where查詢條件,默認為空
// +----------------------------------------------------------------------
// |$pagesize分頁記錄,默認為30
// +----------------------------------------------------------------------
// |使用方法,看上面的控制器調用
// +----------------------------------------------------------------------
public function gclist($field,$where='',$pagesize=30) {
import("ORG.Util.Page");
$count = $this->field('id')->where($where)->count();
$P = new Page($count, $pagesize);
$list=$this->field($field)->where($where)->order('bpath,id')->limit($P->firstRow . ',' . $P->listRows)->select();
foreach ($list as $k => $v) {
$list[$k]['count'] = count(explode('-', $v['bpath']));
$list[$k]['tousername']=$this->where(array('id'=> $v['pid']))->getField('username');
$str = '';
if ($v['pid'] <> 0) {
for ($i = 0; $i < $list[$k]['count'] * 2; $i++) {
$str .= ' ';
}
$str .= ' ';
}
$list[$k]['space'] = $str;
}
$P->setConfig('header', '篇');
$P->setConfig('prev', "«");
$P->setConfig('next', '»');
$P->setConfig('first', '|«');
$P->setConfig('last', '»|');
$page = $P->show();
$arr=array('page'=>$page,'list'=>$list);
return $arr;
}
}
?>
關于怎么在ThinkPHP中利用無限級分類實現一個留言與回復功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。