TP5.0實現無限極回復功能


最近做項目的時候用到了評論回復,使用ThinkPHP5.0框架做回復碰到了一些問題,簡單總結一下。(李昌輝)

1.首先是數據表的設計:

create table zy_huifu
(
	code int auto_increment primary key, #回復代號
	puser varchar(50), #回復人員
	listcode int, #文章代號
	time varchar(50), #回復時間
	content text, #回復內容
	pcode int, #父級代號 0文章
	leval int, #級別 0頂級 1其它
	isok int #已讀未讀0未讀1已讀
);

評論和回復放在了一張表里面,為了在顯示的時候做區分,評論作為頂級回復級別代號為0,其它的子級回復級別代號為1。

每個回復都有一個父級代號代表回復的哪一條評論,如果是直接評論的文章,父級代號設置為0.

2.接下來是在頁面上顯示評論和回復信息:

 

 在控制器里面,我們需要去查詢該文章下的所有評論及回復內容,並且注冊到TP框架里面,這里調用了一個方法CommentList()來獲取該文章下的評論回復:

//查詢評論
$ahuifu = $this->CommentList($code,0);
$this->assign("ahuifu",$ahuifu);

CommentList()方法如下,使用遞歸的方式將所有評論回復按照一定的順序查詢出來並且存儲到數組里面:

//讀取評論列表的遞歸,code為文章代號,pcode為父級代號
    public function CommentList($code,$pcode){
        $commentlist = array(); //存儲評論數組
        $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;
            //查詢子回復
            $zi = $this->CommentList($code,$v["code"]);
            if(count($zi)){
                foreach($zi as $v1){
                    $commentlist[] = $v1;
                }
            }
        }
        return $commentlist;
    }

在view視圖頁面顯示數據:

{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}">回復</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}">回復</button></span>
                    </div>
                    <div class="show_nr_pl_nr">
                        {$vp.content}
                    </div>
                </div>
                </div>
                {/if}
            
{/volist}

3.添加回復及評論

添加評論的時候注意將父級代號pcode添加為0,將級別leval添加為0即可。

添加回復的時候將父級代號添加為要回復的這一條數據的主鍵,將級別leval添加為1即可。

具體實現比較簡單,不贅述。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM