PHP Unlimited Class implementation comments and replies

Source: Internet
Author: User
Often in the major forums or news section of the details of the page to see the comments function, of course, not only directly to the content of the comments to be so simple, you can reply to other people's comments, others can reply to your comments or replies, so repeatedly, theoretically can be said to be no rest, From a technical point of view it is easy to think of the use of infinite classification technology to store data, the use of recursive access to comment-level structure data, using AJAX to achieve Comment page interaction, here with the thinkphp framework to do a simple demo practice practicing, in order to streamline the process here the third level of comment began to stop Of course, as long as the basis of a little modification on the implementation of the unlimited return function, mainly the view layer style changes more trouble, it will take some time. first, the effect needs analysis:

In the head can be directly published level comments, the latest published comments appear on the top, the following effect chart

Comments on the post can be returned, the response displayed in the superior comments below, forming a hierarchical relationship, the following effect chart

Page action details: Click on a comment of the Reply button, display the Reply text input box, while the other comments of the reply text input box disappears, when the Reply button is clicked again, the text box disappears

At the last level of comment (set here is the third level) Close the Reply function to display the total number of comments second, the realization of ideas and details 1. Datasheet design

2.controller layer key function:

(1). Recursive Get comment list

/**
* Recursive get comment list *
   /protected function getcommlist ($parent _id = 0,& $result = Array ()) {       
    $arr = M (' Comment ')->where ("parent_id = '". $parent _id. "") ->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;
   }

(2). Show the action of the comment page

The Public Function index () {  
    $num =  M (' comment ')->count ();//Get the total number
    of comments $this->assign (' num ', $num);
      $data =array ();
    $data = $this->getcommlist ()//Get comment list
    $this->assign ("Commlist", $data);
      $this->display (' index ');
  }

(3). Comment Page Ajax Access Add Comment action

/**
* Add Comment */public
   function addcomment () {            
    $data =array ();
    if ((Isset ($_post["comment"))) && (!empty ($_post["comment"))) {
        $cm = Json_decode ($_post["comment"], true)//through the second argument true, converts the JSON string to the array of key values
        $cm [' Create_time ']=date (' y-m-d h:i:s ', Time ());
        $NEWCM = M (' comment ');
        $id = $newcm->add ($cm);

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

        $num =  M (' comment ')->count ()//Total statistical comments
        $data [' num ']= $num;

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


    echo Json_encode ($data);
   }
3.view Layer Implementation

(1). The overall structure design of the display page


Actual effect:

page HTML code:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.