溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用TP5.0框架怎么實(shí)現(xiàn)一個(gè)無限極回復(fù)功能

發(fā)布時(shí)間:2021-02-08 15:01:46 來源:億速云 閱讀:154 作者:Leah 欄目:開發(fā)技術(shù)

使用TP5.0框架怎么實(shí)現(xiàn)一個(gè)無限極回復(fù)功能?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

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.接下來是在頁(yè)面上顯示評(píng)論和回復(fù)信息:

使用TP5.0框架怎么實(shí)現(xiàn)一個(gè)無限極回復(fù)功能

在控制器里面,我們需要去查詢?cè)撐恼孪碌乃性u(píng)論及回復(fù)內(nèi)容,并且注冊(cè)到TP框架里面,這里調(diào)用了一個(gè)方法CommentList()來獲取該文章下的評(píng)論回復(fù):

//查詢?cè)u(píng)論
$ahuifu = $this->CommentList($code,0);
$this->assign("ahuifu",$ahuifu);

CommentList()方法如下,使用遞歸的方式將所有評(píng)論回復(fù)按照一定的順序查詢出來并且存儲(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;
      //查詢子回復(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" >
          <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即可。

關(guān)于使用TP5.0框架怎么實(shí)現(xiàn)一個(gè)無限極回復(fù)功能問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI