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

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

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

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

最近做項(xiàng)目的時(shí)候用到了評(píng)論回復(fù),使用ThinkPHP5.0框架做回復(fù)碰到了一些問題,簡單總結(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.接下來是在頁面上顯示評(píng)論和回復(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視圖頁面顯示數(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)比較簡單,不贅述。

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論