Home > php教程 > php手册 > body text

thinkphp无限评论

WBOY
Release: 2016-06-07 11:36:06
Original
1176 people have browsed it

使用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元

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!