本文實(shí)例講述了TP5.0框架實(shí)現(xiàn)無(wú)限極回復(fù)功能的方法。分享給大家供大家參考,具體如下:
最近做項(xiàng)目的時(shí)候用到了評(píng)論回復(fù),使用ThinkPHP5.0框架做回復(fù)碰到了一些問(wèn)題,簡(jiǎn)單總結(jié)一下。(李昌輝)
1.首先是數(shù)據(jù)表的設(shè)計(jì):
create table zy_huifu ( code int auto_increment primary key, #回復(fù)代號(hào) puser varchar(50), #回復(fù)人員 listcode int, #文章代號(hào) time varchar(50), #回復(fù)時(shí)間 content text, #回復(fù)內(nèi)容 pcode int, #父級(jí)代號(hào) 0文章 leval int, #級(jí)別 0頂級(jí) 1其它 isok int #已讀未讀0未讀1已讀 );
評(píng)論和回復(fù)放在了一張表里面,為了在顯示的時(shí)候做區(qū)分,評(píng)論作為頂級(jí)回復(fù)級(jí)別代號(hào)為0,其它的子級(jí)回復(fù)級(jí)別代號(hào)為1。
每個(gè)回復(fù)都有一個(gè)父級(jí)代號(hào)代表回復(fù)的哪一條評(píng)論,如果是直接評(píng)論的文章,父級(jí)代號(hào)設(shè)置為0.
2.接下來(lái)是在頁(yè)面上顯示評(píng)論和回復(fù)信息:
在控制器里面,我們需要去查詢(xún)?cè)撐恼孪碌乃性u(píng)論及回復(fù)內(nèi)容,并且注冊(cè)到TP框架里面,這里調(diào)用了一個(gè)方法CommentList()來(lái)獲取該文章下的評(píng)論回復(fù):
//查詢(xún)?cè)u(píng)論 $ahuifu = $this->CommentList($code,0); $this->assign("ahuifu",$ahuifu);
CommentList()方法如下,使用遞歸的方式將所有評(píng)論回復(fù)按照一定的順序查詢(xún)出來(lái)并且存儲(chǔ)到數(shù)組里面:
//讀取評(píng)論列表的遞歸,code為文章代號(hào),pcode為父級(jí)代號(hào) public function CommentList($code,$pcode){ $commentlist = array(); //存儲(chǔ)評(píng)論數(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; //查詢(xún)子回復(fù) $zi = $this->CommentList($code,$v["code"]); if(count($zi)){ foreach($zi as $v1){ $commentlist[] = $v1; } } } return $commentlist; }
在view視圖頁(yè)面顯示數(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ù)及評(píng)論
添加評(píng)論的時(shí)候注意將父級(jí)代號(hào)pcode添加為0,將級(jí)別leval添加為0即可。
添加回復(fù)的時(shí)候?qū)⒏讣?jí)代號(hào)添加為要回復(fù)的這一條數(shù)據(jù)的主鍵,將級(jí)別leval添加為1即可。
具體實(shí)現(xiàn)比較簡(jiǎn)單,不贅述。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:銅陵 重慶 臨沂 鷹潭 衡陽(yáng) 十堰 麗江 巴彥淖爾
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《TP5.0框架實(shí)現(xiàn)無(wú)限極回復(fù)功能的方法分析》,本文關(guān)鍵詞 TP5.0,框架,實(shí)現(xiàn),無(wú)限,極,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。