ThinkPHP无限回复评论功能概述
本实例详细解析了基于ThinkPHP框架开发的无限级回复评论功能的具体实现方案,供开发者参考借鉴。
若仅处理单层回复,逻辑较为简单;但要实现无限层级的嵌套回复,单纯依靠多张数据表难以维系。借助TP框架可高效完成此功能(GitHub源码链接:https://github.com/Jonybin/responsemessage)。下载后需特别注意数据库配置与使用细节。
控制器递归逻辑与视图渲染
控制器的核心逻辑如下:
function CommentList($pid = 0, &$commentList = array(), $spac = 0) {
static $i = 0;
$spac = $spac + 1; //初始为1级评论
$List = M('comment')->
field('id,add_time,author,content,pid')->
where(array('pid' => $pid))->order("id DESC")->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']=$pautor;
$i++;
$this->CommentList($v['id'], $commentList, $spac);
}
return $commentList;
}
视图层的展示部分如下:
{$vo.author}
{$vo.author}回复{$vo.pauthor}
回复{$vo.time|date="Y-m-d",###}
{$vo.content|reFace}
如需获取完整实例代码,请点击此处进行本站下载。
期望上述内容能为使用ThinkPHP框架进行PHP程序设计的读者提供有益帮助。