搜尋
首頁php教程php手册thinkphp无限评论

使用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无限评论

附件 thinkphp无限评论.zip ( 3.01 MB 下载:140 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。