无限级评论

 

<?php
/*
* 无递归实现无限级嵌套评论 By Falcon
* 雕刻时光: 在SAE开的新博客

* Email: admin@falconlab.tk qq:516974090
*
*关键是在数据库里增加一个新字段记录评论条目的路径,


* 如path’=> ‘01,03,05,’ 表示是当前id为5, 父评论id为3,再上一级评论为1 ,诸如此类,前置0是为了方便排序。
*插入评论很简单, 不用说。这里主要操作显示评论的,关键的技术已经表达了,其他的细节忽略,这只是一个DEMO
*fb()和前面两行是debug用的。方便使用firephp的同学Debug。可以忽略。
*思路就是对数组重新分组和排序,能用,但是没有想像中的给力,有小bug, 要改善排序,另外对样式表进行了部分操作~
*使用方法: 下载放服务器目录直接运行
*
* 微博:http://t.qq.com/falcon_chen
*/

require_once(‘FirePHPCore/fb.php’); //firephp,debug用,可去除
ob_start(); //firephp需写入缓冲区,可去除。

//模拟数据库取出的数组,评论时间字段省略

$data = array(
array(‘id’=>1, ‘pid’=>0,’article_id’=>1, ‘path’=> ’01,’, ‘author’=>’falcon’, ‘content’=>’这是评论的内容,content #1 <a href=”http://www.falconlab.tk“>雕刻时光</a>’),
array(‘id’=>2, ‘pid’=>0,’article_id’=>1, ‘path’=> ’02,’, ‘author’=>’chen’, ‘content’=>’content #2’),
array(‘id’=>3, ‘pid’=>1,’article_id’=>1, ‘path’=> ‘01,03,’, ‘author’=>’xx’, ‘content’=>’content #3回复评论#1’),
array(‘id’=>4, ‘pid’=>0,’article_id’=>1, ‘path’=> ’04,’, ‘author’=>’falcon4’, ‘content’=>’content #4’),
array(‘id’=>5, ‘pid’=>3,’article_id’=>1, ‘path’=> ‘01,03,05,’, ‘author’=>’falcon5’, ‘content’=>’content #5,回复评论#3’),
array(‘id’=>6, ‘pid’=>5,’article_id’=>1, ‘path’=> ‘01,03,05,06,’, ‘author’=>’falcon6’, ‘content’=>’content #6,回复评论’),
array(‘id’=>7, ‘pid’=>1,’article_id’=>1, ‘path’=> ‘01,07,’, ‘author’=>’falcon7’, ‘content’=>’content #7,回复评论#1’),
array(‘id’=>8, ‘pid’=>4,’article_id’=>1, ‘path’=> ‘04,08,’, ‘author’=>’falcon’, ‘content’=>’content #8,回复评论#4’),
array(‘id’=>9, ‘pid’=>6,’article_id’=>1, ‘path’=> ‘01,03,05,06,09,’, ‘author’=>’falcon9’, ‘content’=>’content #9,回复评论#6’),
array(‘id’=>10, ‘pid’=>1,’article_id’=>1, ‘path’=> ‘01,10,’, ‘author’=>’falcon10’, ‘content’=>’content #10,回复评论#1’),
array(‘id’=>11, ‘pid’=>3,’article_id’=>1, ‘path’=> ‘01,03,11,’, ‘author’=>’falcon11’, ‘content’=>’content #11,回复评论#3’),
array(‘id’=>12, ‘pid’=>0,’article_id’=>1, ‘path’=> ’12,’, ‘author’=>’falcon12’, ‘content’=>’content #12,原始评论’),
array(‘id’=>13, ‘pid’=>0,’article_id’=>1, ‘path’=> ’13,’, ‘author’=>’falcon’, ‘content’=>’测试超链接:<a href =”http://t.qq.com/falcon_chen“>我那打了酱油的微博</a>’)
);

fb($data,”before sort”);
//先按path排序,如果数据库取得时已经按path排序则可不进行这一步
foreach($data as $row){
$volume[]  = $row[‘path’];
}

/*
*预留这里对$volume 进行自定义排序 使用uasort,否则当前只能对id<100评论有效,当然也可以通过在前面继续加0
*如000将对<1000以内有效。0000则<10000诸如此类
*待解决
*/
array_multisort($volume, SORT_ASC, $data);

fb($data,”after sort1″);
//如01,03,11应排在01,03,05之后,01,07之前
$newData = array();
foreach($data as &$v) {
$split = explode(‘,’,$v[‘path’]) ;
$i = $split[0];
$level = count($split)-1;
$v[‘lv’] = $level;
$newData[$i][] = $v;

}
fb($newData,”after sort2″);

$newData = array_values($newData);
fb($newData,’final ‘);
?>

<?php
/*助手函数*/

//找出父评论的作者
function getAuthor($pid,$arr){
foreach($arr as $k=>$v) {
if($pid == $v [‘id’])
return $arr[$k][‘author’];
}
}
//取得最大级数
function getMaxLevel ($data) {
$max = 0;

foreach ($data as $k =>$v) {
$max = $max>$v[‘lv’] ? $max :$v[‘lv’];
}

return (int)$max;
}
//生成级别样式
function genLvCss() {
$lvStr=’.lv_’;
$lvCss =”;
global $data;
//fb(getMaxLevel($data),’max’);
for($i=2; $i<=getMaxLevel($data);$i++) {
$mlNum =30*($i-1);
$ml =”margin-left:” . $mlNum .”px;”;

$bg = ($i%2==0)?”background-color:#fee;” : “background-color:#fff;”;
$lvCss   .= $lvStr.$i . “{” . $ml .$bg. “}
“;

}
echo $lvCss;
}
?>

<?php /*—————–模板视图————-*/ ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”zh-CN” xml:lang=”zh-CN”>
<head>
<title>嵌套评论列表</title>
<style>
h1 {
margin-left:100px;
}
h2 {
font-size:14px;
margin:0;
padding:0;
}
.main {
/*border:2px solid #000;*/
margin-bottom:20px;
width:400px;
/*background-color:#ffc;*/
padding: 5px 10px 20px 10px;
border:3px double #000;

}
.level  {
font-size:12px;
border:1px dotted #000;
padding:5px;
border-spacing:0;
margin-bottom:3px;

}
.lv_1{
margin-left:0px;
color:#000;
background-color:#fec;
border:1px solid #000;

}

.re_comment {
text-decoration:underline;
display:inline;
font-style:italic;
color:#f00;
}
.comment_content {
padding-left: 20px;
}
</style>
<style>
<?php
//生成按级别层叠的样式
genLvCss();
?>
</style>
</head>

<h1>嵌套评论列表</h1>
<body>
<?php foreach ($newData As $k => $rows) : ?>
<div class = “main”>
<h2><?php echo “#”.($k+1). “楼”; ?></h2>
<?php foreach($rows as $k=>$row): ?>
<div class = “level lv_<?php echo  $row[‘lv’]?>”>
<?php echo $row[‘author’] ?>
<?php if ($k ==0) : ?>说 :
<?php else :?>回复#
<div class = “re_comment”>
<?php echo getAuthor($row[‘pid’],$rows)?>
:</div>
<?php endif ?>

<div class = “comment_content”><?php echo $row[‘content’] ?></div>
</div>
<?php endforeach;  ?>
</div>
<?php endforeach; ?>
</body>
</html>

 

- EOF -