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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
