温馨提示  2024年 6月我们已经停止开发者板块文章内容更新,谢谢来访。存档数据
知识 2023-08-25 56 次阅读

php怎么实现多次回复

   

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视频教程》