使用thinkphp和递归算法写的一个无限评论
无限评论采用的递归算法遍历评论
数据表结构为:
CREATE TABLE `blog_comment` (
`id` int(10) NOT NULL,
`content` varchar(500) NOT NULL,
`pid` int(10) NOT NULL,
`email` varchar(50) NOT NULL,
`add_time` int(30) NOT NULL,
`author` varchar(30) NOT NULL,
`isShow` int(1) NOT NULL,
`ip` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
其中:id为本条评论id,pid为上级评论id,(即本条评论是回复哪一条评论的)
本算法思路,将pid初始化为0,代表一级评论,先查询1级评论,然后再将1级评论的id赋值给pid,调用自身算法遍历1级评论下面的回复,以此类推,一直循环调用自身算法,遍历下一级评论;
算法为:
//评论列表
//评论列表
function CommentList($pid=0,&$commentList=array(),$spac=0,$pauthor=NULL){
static $i=0;
$spac=$spac+1;//初始为1级评论
$pauthor=$pauthor;
$List=M('comment')->
field('blog_comment.id,blog_comment.add_time,blog_comment.author,blog_comment.content,pid,blog_comment.id,blog_comment.pid')-> where(array('blog_comment.pid'=>$pid))->select();
foreach($List as $k=>$v){
$commentList[$i]['level']=$spac;//评论层级
$commentList[$i]['author']=$v['author'];
$commentList[$i]['id']=$v['id'];
$commentList[$i]['pid']=$v['pid'];//此条评论的父id
$commentList[$i]['content']=$v['content'];
$commentList[$i]['time']=$v['add_time'];
$commentList[$i]['pauthor']=$pauthor;//此条评论是回复谁的
$i++;
$this->CommentList($v['id'],$commentList,$spac,$v['author']);
}
return $commentList;
}
最后结果:
thinkphp无限评论.zip
( 3.01 MB 下载:140 次 )
AD:真正免费,域名+虚机+企业邮箱=0元

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

ZendStudio 13.5.1 Mac
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Herunterladen der Mac-Version des Atom-Editors
Der beliebteste Open-Source-Editor

Sicherer Prüfungsbrowser
Safe Exam Browser ist eine sichere Browserumgebung für die sichere Teilnahme an Online-Prüfungen. Diese Software verwandelt jeden Computer in einen sicheren Arbeitsplatz. Es kontrolliert den Zugriff auf alle Dienstprogramme und verhindert, dass Schüler nicht autorisierte Ressourcen nutzen.

SublimeText3 Linux neue Version
SublimeText3 Linux neueste Version

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen