php method to implement multiple replies: 1. Create "function commentList($aid,$pid = 0,&$result=array()){...}"; 2. Pass "$this ->commentList($aid);" can be called.
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
How to achieve multiple replies in php?
PHP unlimited comment reply function implementation
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; }
Calling method
$this->commentList($aid);
Using tp5 to write article comment reply function in the project
The pid is used in the table to identify the id of the reply table. The table structure is as follows
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='文章评论表'
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to achieve multiple replies in php. For more information, please follow other related articles on the PHP Chinese website!