thinkPHP realizes comment reply function based on ajax
- 2021-10-24 19:10:59
- OfStack
In this paper, an example is given to describe the implementation of comment reply function based on ajax by thinkPHP. Share it for your reference, as follows:
Controller code:
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$num = M('comment')->count(); // Get the total number of comments
$this->assign('num',$num);
$data=array();
$data=$this->getCommlist();// Get a list of comments
$this->assign("commlist",$data);
$this->display('index');
}
/**
* Add a comment
*/
public function addComment(){
$data=array();
if((isset($_POST["comment"]))&&(!empty($_POST["comment"]))){
$cm = json_decode($_POST["comment"],true);// Adopt the 2 Parameters true , will json String to an array of key-value pairs
$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 number of statistical comments
$data['num']= $num;
}else{
$data["error"] = "0";
}
echo json_encode($data);
}
/**
* Recursively get a 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;
}
}
JavaScript code:
$(function(){
// Click to submit comments
$('body').delegate('.comment-submit','click',function(){
var content = $.trim($(this).parent().prev().children("textarea").val());// Get the current comment content according to the layout structure
$(this).parent().prev().children("textarea").val("");// Empty the input box after obtaining the content
if(""==content){
alert(" Comment content cannot be empty !");
}else{
var cmdata = new Object();
cmdata.parent_id = $(this).attr("parent_id");// Superior comments id
cmdata.content = content;
cmdata.nickname = " Tourists ";// Test data
cmdata.head_pic = "/Public/images/default.jpg";// Test data
var replyswitch = $(this).attr("replyswitch");// Gets the Reply Switch Lock property
$.ajax({
type:"POST",
url:"/index.php/home/index/addComment",
data:{
comment:JSON.stringify(cmdata)
},
dataType:"json",
success:function(data){
if(typeof(data.error)=="undefined"){
$(".comment-reply").next().remove();// Delete all existing replies div
// Total number of update comments
$(".comment-num").children("span").html(data.num+" A comment ");
// Show new comments
var newli = "";
if(cmdata.parent_id == "0"){
// Published is 1 Level comments, add to the 1 Grade ul In the list
newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"' alt=''></div><div class='cm'><div class='cm-header'><span>"+data.nickname+"</span><span>"+data.create_time+"</span></div><div class='cm-content'><p>"+data.content+"</p></div><div class='cm-footer'><a class='comment-reply' comment_id='"+data.id+"' href='javascript:void(0);'> Reply </a></div></div></div><ul class='children'></ul></li>";
$(".comment-ul").prepend(newli);
}else{
// Otherwise, it is added to the corresponding child ul In the list
if('off'==replyswitch){// The existence of the reply close lock is verified, that is, 3 Level comments no longer provide reply function
newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"' alt=''></div><div class='children-cm'><div class='cm-header'><span>"+data.nickname+"</span><span>"+data.create_time+"</span></div><div class='cm-content'><p>"+data.content+"</p></div><div class='cm-footer'></div></div></div><ul class='children'></ul></li>";
}else{//2 The reply button of level comment should add the reply closing lock attribute
newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"' alt=''></div><div class='children-cm'><div class='cm-header'><span>"+data.nickname+"</span><span>"+data.create_time+"</span></div><div class='cm-content'><p>"+data.content+"</p></div><div class='cm-footer'><a class='comment-reply' comment_id='"+data.id+"' href='javascript:void(0);' replyswitch='off' > Reply </a></div></div></div><ul class='children'></ul></li>";
}
$("li[comment_id='"+data.parent_id+"']").children("ul").prepend(newli);
}
}else{
// There is an error message
alert(data.error);
}
}
});
}
});
// Click " Reply " Button to show or hide the reply input box
$("body").delegate(".comment-reply","click",function(){
if($(this).next().length>0){// Judge the reply div Already exists , To get rid of
$(this).next().remove();
}else{// Add Reply div
$(".comment-reply").next().remove();// Delete all existing replies div
// Add current reply div
var parent_id = $(this).attr("comment_id");// Comments to reply to id
var divhtml = "";
if('off'==$(this).attr("replyswitch")){//2 After the reply to the level comment, 3 Level comments no longer provide reply function , Attach the closing property to the " Submit a reply " Button "
divhtml = "<div class='div-reply-txt' style='width:98%;padding:3px;' replyid='2'><div><textarea class='txt-reply' replyid='2' style='width: 100%; height: 60px;'></textarea></div><div style='margin-top:5px;text-align:right;'><a class='comment-submit' parent_id='"+parent_id+"' style='font-size:14px;text-decoration:none;background-color:#63B8FF;' href='javascript:void(0);' replyswitch='off' > Submit a reply </a></div></div>";
}else{
divhtml = "<div class='div-reply-txt' style='width:98%;padding:3px;' replyid='2'><div><textarea class='txt-reply' replyid='2' style='width: 100%; height: 60px;'></textarea></div><div style='margin-top:5px;text-align:right;'><a class='comment-submit' parent_id='"+parent_id+"' style='font-size:14px;text-decoration:none;background-color:#63B8FF;' href='javascript:void(0);'> Submit a reply </a></div></div>";
}
$(this).after(divhtml);
}
});
})
Page style code:
.comment-filed{
width:640px;
margin:0 auto;
}
.comment-num{
text-align: right;
font-size:14px;
}
.div-txt-submit{
text-align:right;
margin-top:8px;
}
.comment-submit{
background-color:#63B8FF;
margin-top:15px;
text-decoration:none;
color:#fff;
padding:5px;
font-size:14px;
}
.txt-commit{
border:1px solid blue;
width:620px;
height: 60px;
padding: 10px;
}
.txt-reply{
width: 100%;
height: 60px;
}
.comment-filed-list{
margin-top:20px;
}
.comment-list{
margin-top:2px;
width:herit;
height:50px;
border-top:1px solid gray;
}
.comment-ul{
list-style:none;
padding-left:0;
}
.head-pic{
width:40px;
height:40px;
}
.cm{
position:relative;
top:0px;
left:40px;
top:-40px;
width:600px;
}
.cm-header{
padding-left:5px;
}
.cm-content{
padding-left:5px;
}
.cm-footer{
padding-bottom:15px;
text-align:right;
border-bottom: 1px dotted #CCC;
}
.comment-reply{
text-decoration:none;
color:gray;
font-size: 14px;
}
.children{
list-style:none;
background-color:#FAFAFA;
padding-left:0;
margin-left:40px;
}
.children-cm{
position:relative;
left:40px;
top:-40px;
width:90%;
}
Page layout code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>php Comment and reply function </title>
<link rel="stylesheet" type="text/css" href="/Public/css/comment.css" rel="external nofollow" >
<script type="text/javascript" src="/Public/js/jquery-1.11.3.min.js" ></script>
<script type="text/javascript" src="/Public/js/comment.js" ></script>
</head>
<body>
<div class="comment-filed">
<!-- Comment area begin-->
<div>
<div class="comment-num">
<span>{$num} A comment </span>
</div>
<div>
<div>
<textarea class="txt-commit" replyid="0"></textarea>
</div>
<div class="div-txt-submit">
<a class="comment-submit" parent_id="0" style="" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span style=''> Make a comment </span></a>
</div>
</div>
</div>
<!-- Comment area end-->
<!-- Comment list display area begin-->
<!-- {$commentlist} -->
<div class="comment-filed-list" >
<div><span> All comments </span></div>
<div class="comment-list" >
<!--1 Level comment list begin-->
<ul class="comment-ul">
<volist name="commlist" id="data">
<li comment_id="{$data.id}">
<div >
<div>
<img class="head-pic" src="{$data.head_pic}" alt="">
</div>
<div class="cm">
<div class="cm-header">
<span>{$data.nickname}</span>
<span>{$data.create_time}</span>
</div>
<div class="cm-content">
<p>
{$data.content}
</p>
</div>
<div class="cm-footer">
<a class="comment-reply" comment_id="{$data.id}" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > Reply </a>
</div>
</div>
</div>
<!--2 Level comments begin-->
<ul class="children">
<volist name="data.children" id="child" >
<li comment_id="{$child.id}">
<div >
<div>
<img class="head-pic" src="{$child.head_pic}" alt="">
</div>
<div class="children-cm">
<div class="cm-header">
<span>{$child.nickname}</span>
<span>{$child.create_time}</span>
</div>
<div class="cm-content">
<p>
{$child.content}
</p>
</div>
<div class="cm-footer">
<a class="comment-reply" replyswitch="off" comment_id="{$child.id}" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > Reply </a>
</div>
</div>
</div>
<!--3 Level comments begin-->
<ul class="children">
<volist name="child.children" id="grandson" >
<li comment_id="{$grandson.id}">
<div >
<div>
<img class="head-pic" src="{$grandson.head_pic}" alt="">
</div>
<div class="children-cm">
<div class="cm-header">
<span>{$grandson.nickname}</span>
<span>{$grandson.create_time}</span>
</div>
<div class="cm-content">
<p>
{$grandson.content}
</p>
</div>
<div class="cm-footer">
<!-- <a class="comment-reply" comment_id="1" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > Reply </a> -->
</div>
</div>
</div>
</li>
</volist>
</ul>
<!--3 Level comments end-->
</li>
</volist>
</ul>
<!--2 Level comments end-->
</li>
</volist>
</ul>
<!--1 Level comment list end-->
</div>
</div>
<!-- Comment list display area end-->
</div>
</body>
</html>
sql statement:
DROP TABLE IF EXISTS `t_comment`;
CREATE TABLE `t_comment` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ' Primary key id',
`parent_id` int(11) NOT NULL COMMENT ' Superior comments id, If 1 Level comments are 0',
`nickname` varchar(100) DEFAULT NULL COMMENT ' Commentator nickname ',
`head_pic` varchar(400) DEFAULT NULL COMMENT ' Avatar of commentator ',
`content` text COMMENT ' Comment content ',
`create_time` datetime DEFAULT NULL COMMENT ' Comment or reply publication time ',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=148 DEFAULT CHARSET=utf8;
One less jquery. js for page layout. Please add it yourself.
Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.