Php unlimited classification practices-comment and reply functions

Source: Internet
Author: User
Php unlimited classification practices-comment and reply functions
The comment function is often seen below the details pages of major forums or news sections. of course, it is not just as simple as directly posting comments, and you can reply to others' comments, others can review or reply to your reply again. In theory, there is no limit. From a technical point of view, it is easy to think of using the unlimited classification technology to store data, use recursion to obtain comment-level structure data and ajax to implement comment-page interaction. here we use thinkphp framework as a simple demo trainer. in order to simplify the process, Level 3 comments no longer provide the reply function, of course, you only need to make a slight modification on this basis to implement the unlimited reply function, mainly because it takes some time to modify the view layer style. ** 1. performance requirement analysis: ** 1. you can directly post a level-1 comment in the header. The latest comment is shown at the top! [Write the image description here] ( http://img.blog.csdn.net/20150517173206601 ) 2. you can reply to a published comment. The reply is displayed under the comment of the superior, forming a hierarchical relationship! [Write the image description here] ( http://img.blog.csdn.net/20150517173710459 ) 3. page operation details: When you click the reply button of a comment, the reply text input box is displayed, and the reply text input box of other comments disappears. when you click the reply button again, the text box disappears. disable the reply function in the last-level comment (set to level 3 here. dynamically display the total number of comments ** 2. implementation ideas and details: ** 1. data table design! [Comment table] ( http://img.blog.csdn.net/20150517181335995 ) 2. key functions at the controller layer: ①. recursively retrieve comments list "/*** recursively retrieve comments 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;} "②. display a on the comment page Ction "public function index () {$ num = M ('Comment')-> count (); // gets the total number of comments $ this-> assign ('num ', $ num); $ data = array (); $ data = $ this-> getCommlist (); // get the comment list $ this-> assign ("commlist ", $ data); $ this-> display ('index');} "③. 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); // use the second parameter to true, convert a json string to a key-value pair array $ 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 (); // count the total number of comments $ data ['num'] = $ num ;} else {$ data ["error"] = "0";} echo json_encode ($ data);} "3. view layer implementation ①. display the overall structure design of the page! [Write the image description here] ( http://img.blog.csdn.net/20150517200742110 ) Actual results :! [Write the image description here] ( http://img.blog.csdn.net/20150517205141806 ) Page html code:
    
     Php unlimited classification practices ???? Comment and reply    
     

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.