Example of unlimited reply comment function implemented by thinkPHP framework

  • 2021-10-15 10:01:00
  • OfStack

In this paper, an example is given to describe the unlimited reply and comment function realized by thinkPHP framework. Share it for your reference, as follows:

If it is just a simple comment with a single reply, it is very simple to do so. But the question is how to achieve unlimited reply comments! So if it is only a single reply, it is impossible to build a lot of data tables. Then use TP framework to realize unlimited reply comments (GitHub source code download address: https://github.com/Jonybin/responsemessage). Pay attention to the use of database after downloading.

control controller section:


function CommentList($pid = 0, &$commentList = array(), $spac = 0) {
    static $i = 0;
    $spac = $spac + 1; // Initially 1 Level comments 
    $List = M('comment')->
        field('id,add_time,author,content,pid')->
        where(array('pid' => $pid))->order("id DESC")->select();
    foreach ($List as $k => $v) {
      $commentList[$i]['level'] = $spac; // Comment hierarchy 
      $commentList[$i]['author'] = $v['author'];
      $commentList[$i]['id'] = $v['id'];
      $commentList[$i]['pid'] = $v['pid']; // The father of this comment id
      $commentList[$i]['content'] = $v['content'];
      $commentList[$i]['time'] = $v['add_time'];
      // $commentList[$i]['pauthor']=$pautor;
      $i++;
      $this->CommentList($v['id'], $commentList, $spac);
    }
    return $commentList;
}

view view section:


<volist name="commentList" id="vo">
 <eq name="vo.pid" value="0"><hr class="solidline"/><else/><hr class="dottedline"/></eq>
 <div class="commentList " style="padding-left:{$vo['level']-1}cm">
   <div><span class="user">
   <if condition="($vo.pauthor eq NULL)">{$vo.author}
  <else /> {$vo.author}<span class="black" style="color: #000101"> Reply </span>{$vo.pauthor}
   </if>
 </span><a class="hf" id="{$vo.id}" style="float: right"> Reply </a><span class="hftime">{$vo.time|date="Y-m-d",###}</span></div>
   <div class="content">{$vo.content|reFace}</div>
 </div>
</volist>

The complete example code can be downloaded here.

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: