本文實(shí)例講述了TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法。分享給大家供大家參考,具體如下:
最近做項(xiàng)目的時候用到了評論回復(fù),使用ThinkPHP5.0框架做回復(fù)碰到了一些問題,簡單總結(jié)一下。(李昌輝)
1.首先是數(shù)據(jù)表的設(shè)計:
create table zy_huifu
(
code int auto_increment primary key, #回復(fù)代號
puser varchar(50), #回復(fù)人員
listcode int, #文章代號
time varchar(50), #回復(fù)時間
content text, #回復(fù)內(nèi)容
pcode int, #父級代號 0文章
leval int, #級別 0頂級 1其它
isok int #已讀未讀0未讀1已讀
);
評論和回復(fù)放在了一張表里面,為了在顯示的時候做區(qū)分,評論作為頂級回復(fù)級別代號為0,其它的子級回復(fù)級別代號為1。
每個回復(fù)都有一個父級代號代表回復(fù)的哪一條評論,如果是直接評論的文章,父級代號設(shè)置為0.
2.接下來是在頁面上顯示評論和回復(fù)信息:
![](http://img.jbzj.com/file_images/article/201905/201954121523783.png?20194412168)
在控制器里面,我們需要去查詢該文章下的所有評論及回復(fù)內(nèi)容,并且注冊到TP框架里面,這里調(diào)用了一個方法CommentList()來獲取該文章下的評論回復(fù):
//查詢評論
$ahuifu = $this->CommentList($code,0);
$this->assign("ahuifu",$ahuifu);
CommentList()方法如下,使用遞歸的方式將所有評論回復(fù)按照一定的順序查詢出來并且存儲到數(shù)組里面:
//讀取評論列表的遞歸,code為文章代號,pcode為父級代號
public function CommentList($code,$pcode){
$commentlist = array(); //存儲評論數(shù)組
$list = Db::table("zy_huifu")
->alias('a')
->where("listcode",$code)
->where("pcode",$pcode)
->join("zy_user b","a.puser = b.uid")
->select();
foreach($list as $v){
$commentlist[] = $v;
//查詢子回復(fù)
$zi = $this->CommentList($code,$v["code"]);
if(count($zi)){
foreach($zi as $v1){
$commentlist[] = $v1;
}
}
}
return $commentlist;
}
在view視圖頁面顯示數(shù)據(jù):
{volist name="ahuifu" id="vp"}
{if condition="($vp.leval == 0)"}
div class="panel panel-default pl_list">
div class="panel-body pl_list_nr">
div class="show_nr_pl_tou">
img src="{$vp.img}" width="30" height="30" /> nbsp;
span>{$vp.name}/span>nbsp;
span>{$vp.time|date="Y-m-d H:i:s",###}/span>nbsp;
span>button class="btn btn-primary btn-xs show_huifu_btn" pcode="{$vp.code}">回復(fù)/button>/span>
/div>
div class="show_nr_pl_nr">
{$vp.content}
/div>
/div>
/div>
{else /}
div class="panel panel-default pl_list">
div class="panel-body pl_list_nr" style="margin-left:50px">
div class="show_nr_pl_tou">
img src="{$vp.img}" width="30" height="30" /> nbsp;
span>{$vp.name}/span>nbsp;
span>{$vp.time|date="Y-m-d H:i:s",###}/span>nbsp;
span>button class="btn btn-primary btn-xs show_huifu_btn" pcode="{$vp.code}">回復(fù)/button>/span>
/div>
div class="show_nr_pl_nr">
{$vp.content}
/div>
/div>
/div>
{/if}
{/volist}
3.添加回復(fù)及評論
添加評論的時候注意將父級代號pcode添加為0,將級別leval添加為0即可。
添加回復(fù)的時候?qū)⒏讣壌柼砑訛橐貜?fù)的這一條數(shù)據(jù)的主鍵,將級別leval添加為1即可。
具體實(shí)現(xiàn)比較簡單,不贅述。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- ThinkPHP無限級分類原理實(shí)現(xiàn)留言與回復(fù)功能實(shí)例
- thinkphp實(shí)現(xiàn)無限分類(使用遞歸)
- thinkPHP實(shí)現(xiàn)遞歸循環(huán)欄目并按照樹形結(jié)構(gòu)無限極輸出的方法
- ThinkPHP自動填充實(shí)現(xiàn)無限級分類的方法
- 解析thinkphp的左右值無限分類
- 使用ThinkPHP的自動完成實(shí)現(xiàn)無限級分類實(shí)例詳解
- Thinkphp無限級分類代碼
- thinkPHP框架實(shí)現(xiàn)的無限回復(fù)評論功能示例
- thinkphp5實(shí)現(xiàn)無限級分類
- PHP實(shí)現(xiàn)的無限分類類庫定義與用法示例【基于thinkPHP】
- thinkphp5使用無限極分類