How to achieve multiple replies in php

藏色散人
Release: 2023-03-14 11:40:01
Original
2040 people have browsed it

php method to implement multiple replies: 1. Create "function commentList($aid,$pid = 0,&$result=array()){...}"; 2. Pass "$this ->commentList($aid);" can be called.

How to achieve multiple replies in php

#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;
}
Copy after login

Calling method

$this->commentList($aid);
Copy after login

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='文章评论表'
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template