欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法分析

 更新時間:2019年05月04日 12:18:01   作者:Alvin-靈心  
這篇文章主要介紹了TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法,結(jié)合實(shí)例形式分析了thinkPHP5.0框架下無限極回復(fù)功能相關(guān)的數(shù)據(jù)庫、評論功能及界面布局實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例講述了TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法。分享給大家供大家參考,具體如下:

最近做項目的時候用到了評論回復(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ù)信息:

在控制器里面,我們需要去查詢該文章下的所有評論及回復(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è)計有所幫助。

相關(guān)文章

最新評論