Home > Backend Development > PHP Tutorial > PHP unlimited classification practice - comment and reply function_PHP tutorial

PHP unlimited classification practice - comment and reply function_PHP tutorial

WBOY
Release: 2016-07-13 09:53:27
Original
1281 people have browsed it

PHP unlimited classification practice - comment and reply function

You often see the comment function under the details page of major forums or news sections. Of course, it is not just as simple as posting a comment directly. You can reply to other people's comments, and others can comment or reply to your reply again, and so on. Theoretically, it can be said to be endless. From a technical perspective, it is easy to think of using infinite classification technology to store data, using recursion to obtain comment hierarchical structure data, and using ajax to realize comment page interaction. Here I use the thinkphp framework to make a simple demo to practice. In order to simplify the process, the third-level comments no longer provide a reply function. Of course, as long as a few modifications are made on this basis, the unlimited reply function can be realized. The main reason is that modifying the view layer style is troublesome and takes some time.

1. Effect demand analysis:

You can post first-level comments directly in the header, and the latest comments are displayed at the top, as shown in the following rendering
PHP unlimited classification practice - comment and reply function_PHP tutorial

You can reply to posted comments, and the replies are displayed below the superior comments, forming a hierarchical relationship, as shown in the following rendering
PHP unlimited classification practice - comment and reply function_PHP tutorial

Page operation details: When the reply button of a comment is clicked, the reply text input box is displayed, and the reply text input boxes of other comments disappear. When the reply button is clicked again, the text box disappears

Turn off the reply function at the last level of comment (the setting here is the third level)

Instantly display the total number of comments

2. Implementation ideas and details

1.Data table design

PHP unlimited classification practice - comment and reply function_PHP tutorial

2. Key functions of the controller layer:

(1). Recursively obtain the comment list

<code class="hljs" php="">/**
*递归获取评论列表
   */
   protected function getCommlist($parent_id = 0,&$result = array()){       
    $arr = M(&#39;comment&#39;)->where(parent_id = &#39;.$parent_id.&#39;)->order(create_time desc)->select();   
    if(empty($arr)){
        return array();
    }
    foreach ($arr as $cm) {  
        $thisArr=&$result[];
        $cm[children] = $this->getCommlist($cm[id],$thisArr);    
        $thisArr = $cm;                                    
    }
    return $result;
   }</code>
Copy after login

(2). Show the action of the comment page

<code class="hljs" lasso="">public function index(){  
    $num =  M(&#39;comment&#39;)->count(); //获取评论总数
    $this->assign(&#39;num&#39;,$num);
      $data=array();
    $data=$this->getCommlist();//获取评论列表
    $this->assign(commlist,$data);
      $this->display(&#39;index&#39;);
  }</code>
Copy after login

(3). Comment page ajax access action to add comments

<code class="hljs" php="">/**
*添加评论
   */
   public function addComment(){            
    $data=array();
    if((isset($_POST[comment]))&&(!empty($_POST[comment]))){
        $cm = json_decode($_POST[comment],true);//通过第二个参数true,将json字符串转化为键值对数组
        $cm[&#39;create_time&#39;]=date(&#39;Y-m-d H:i:s&#39;,time());
        $newcm = M(&#39;comment&#39;);
        $id = $newcm->add($cm);

        $cm[id] = $id;
        $data = $cm;

        $num =  M(&#39;comment&#39;)->count();//统计评论总数
        $data[&#39;num&#39;]= $num;

    }else{
        $data[error] = 0;
    }


    echo json_encode($data);
   }</code>
Copy after login

3.View layer implementation

(1). Display the overall structural design of the page

PHP unlimited classification practice - comment and reply function_PHP tutorial
Actual effect:
PHP unlimited classification practice - comment and reply function_PHP tutorial
Page html code:

<code class="hljs" xml="">
</code>
Copy after login
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template