PHP Development Article Comment system

Source: Internet
Author: User
Tags add time php language
Recently work needs to complete a comment on the function of the Internet to find a few comment system display style. Finally, referring to the comment system such as "multi-Talk" and "Chang-yan", I realized a simple comment system using PHP language. It also records the implementation of two ways (recursive and non-recursive), and analyzes the pros and cons of the two approaches, but how the front-end is implemented does not show.

First design the database as follows:

Create TABLE ' comments ' (' id ' bigint unsigned NOT null auto_increment, ' arc_id ' bigint unsigned not null COMMENT ' article ID ', ' use r_id ' bigint unsigned NOT null COMMENT ' user ID ', ' comment_id ' bigint unsigned NOT null DEFAULT ' 0 ' COMMENT ' reply to the ID of a comment ', ' conte NT ' varchar (255) NOT null default ' COMMENT ' comment or reply content ', ' add_time ' timestamp not null default Current_timestamp COMMENT ' Tim Add time ', PRIMARY key (' ID '), key ' arc_id ' (' arc_id ')) engine=innodb DEFAULT Charset=utf8 COMMENT ' article comment form ';

Create the test data as follows:


The implementation scenario is as follows (implemented on the thinkphp framework):
1. Recursive method
Pros: The code is simple to implement, and if the level of comment is fixed at 5 levels, it is recommended that this approach be used so that the front end is simple to achieve with this kind of data result.
Disadvantage: If the level of the comment is not fixed, the front end will not be able to display the comment information, and if the hierarchy is too much, it will greatly consume memory, but also the most fatal is to query the database every time, the performance will be greatly reduced.

/** * @param $arc _id   Article ID * @param int $comm _id   Comment ID * @param array $result * @return Array */function getcommlist ($arc _id, $comm _id = 0, & $result = Array ()) {   //Get comment list if (empty ($arc _id)) {return array ();} $_where = "arc_id = {$arc _id} and comment_id = {$comm _id}"; $res = M (' comments ')->where ($_where)->order (' Add_time DE SC ')->select (); if (empty ($res)) {return array ();} foreach ($res as $cm) {$THISARR = & $result []; $cm ["_child"] = Getcommlist ($arc _id, $cm [' id '], $THISARR); $thisArr = $cm;} return $result;}

Some of the data are shown below:



2, non-recursive method (stack mode implementation)
Advantage: Query database only once, the performance is good. can implement N-level comments, the front end can also be very good display
Cons: The code is slightly more complex and the front-end comment is more complex for fixed-level reviews.

/** * @param $arc _id  Article ID * @return array */public function getcommlist ($arc _id) {if (Empty ($arc _id)) {return array ();} $res = M (' comments ')->where (Array (' arc_id ' = + $arc _id))->order (' Add_time ASC ')->select (); $dataList = $ stack = Array (), if ($res) {foreach ($res as $k = + $v) {   //The data of the comment is first put into storage (that is, comment_id=0) if ($v [' comment_id '] = = 0) {$v [' _ Level '] = 0;   Set number of levels $v[' _root '] = $v [' id '];   Identity Review Idarray_push ($stack, $v);   Into the stack unset ($res [$k]);}} while (!empty ($stack)) {$node = Array_pop ($stack);   Stack $datalist[] = $node; foreach ($res as $_k=>$_v) {if ($_v[' comment_id ') = = $node [' id ']) {$_v[' _level '] = $node [' _ Level ']+1;   Set number of levels $_v[' _root '] = $node [' _root '];   Identity Review Idarray_push ($stack, $_v);   Into the stack unset ($res [$_k]);}}} return $dataList;}

The results of the data display are as follows:

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.