php实现多次回复的方法:1、创建“function commentlist($aid,$pid = 0,&$result=array()){...}”;2、通过“$this->commentlist($aid);”方式调用即可。
本文操作环境:windows7系统,php7.4版,dell g3电脑。
php怎么实现多次回复?
php无限级评论回复功能实现
protected function commentlist($aid,$pid = 0,&$result=array()){ $arr = articlecomment::relation(['usertalent'=> function($query){ $query->field('id,talent_usernickname,talent_avatar'); }])->where(['pid' => $pid])->where(['article_id' => $aid])->order('id desc')->select(); if(empty($arr)){ return array(); } foreach ($arr as $cm) { $thisarr=&$result[]; $cm[children] = $this->commentlist($aid,$cm[id],$thisarr); $thisarr = $cm; } return $result;}
调用方法
$this->commentlist($aid);
项目中使用tp5写文章评论回复功能
表中使用pid来标识回复表的id 表结构如下
create table `bcpub_article_comment` (`id` int(11) unsigned not null auto_increment,`author_id` int(11) unsigned not null default '0' comment '作者id',`article_id` int(11) unsigned not null default '0' comment '文章id',`pid` int(11) unsigned not null default '0',`uid` int(11) unsigned not null default '0' comment '评论人id',`comment` varchar(250) not null default '',`give_count` int(10) unsigned not null default '0' comment '评论点赞数量',`add_time` int(10) unsigned not null default '0',primary key (`id`),key `author_id` (`author_id`),key `pid` (`pid`)) engine=myisam auto_increment=97 default charset=utf8 comment='文章评论表'
推荐学习:《php视频教程》
以上就是php怎么实现多次回复的详细内容。