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" >