大家可能都看过网易评论的那种盖楼式的引用,这边文章就用php和mysql来实现这种效果。
先设计数据表来存放评论数据:
DROP TABLE IF EXISTS `comment`; CREATE TABLE `comment` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL, `site` varchar(128) DEFAULT NULL, `content` varchar(1000) NOT NULL, `time` datetime NOT NULL, `pid` int(10) NOT NULL, `articleid` int(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;其中的关键字段就是pid,保存的是引用评论的id,如果没有引用那么设为0即可。
效果图:
具体的实现主要是通过递归算法来找出所有引用的评论(和无限分类菜单类似),代码如下:
<?php
require_once
"sqlHelper.class.php"
;
//连接数据库
$sqlHelper
=
new
SqlHelper(
'localhost'
,
'root'
,
'root'
,
'test'
, 3306);
//插入评论数据
if
(isset(
$_POST
) && !
empty
(
$_POST
)){
$comment
=
array
();
$comment
[
'username'
] =
$_POST
[
'name'
]?
$_POST
[
'name'
]:
'匿名'
;
$comment
[
'site'
] =
$_POST
[
'site'
]?
$_POST
[
'site'
]:
' '
;
$comment
[
'pid'
] =
$_POST
[
'commentid'
]?
$_POST
[
'commentid'
]:
'0'
;
if
(
$comment
[
'pid'
] ==
$comment
[
'id'
]){
$comment
[
'pid'
] =
'0'
;
}
$comment
[
'content'
] =
$_POST
[
'content'
]?
$_POST
[
'content'
]:
'...'
;
$comment
[
'articleid'
] =
$_POST
[
'articleid'
]?
$_POST
[
'articleid'
]:
'1'
;
$comment
[
'time'
] =
date
(
'Y-m-d H:i:s'
, time () );
$sql_str
=
$sqlHelper
->get_replace_db_sql(
"comment"
,
$comment
);
//echo $sql_str;
$sqlHelper
->insert_db(
$sql_str
);
}
?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
xml:lang=
"en"
>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html;charset=UTF-8"
/>
<title>文章评论的无限引用实现</title>
<style type=
"text/css"
>
*{margin:0;padding:0;}
body{margin:10px;font-size:14px;font-family:宋体}
h1{font-size:26px;margin:10px 0 15px;}
#commentHolder{width:540px;border-bottom:1px solid #aaa;}
.comment{padding:5px 8px;background:#f8fcff;border:1px solid #aaa;font-size:14px;border-bottom:none;}
.comment p{padding:5px 0;}
.comment p.title{color:#1f3a87;font-size:12px;}
.comment p span{float:right;color:#666}
.comment div{background:#ffe;padding:3px;border:1px solid #aaa;line-height:140%;margin-bottom:5px;}
.comment div span{color:#1f3a87;font-size:12px;}
</style>
</head>
<body>
<?php
function
p(
$str
){
echo
"<pre>"
;
if
(
is_array
(
$str
))
print_r(
$str
);
else
echo
$str
;
echo
"</pre>"
;
}
/*
获取文章id对应的所有评论,以数组的形式返回结果
*/
function
getCommentByArticleId(
$articleid
=1){
$result
=
array
();
if
(
empty
(
$articleid
) || !
is_numeric
(
$articleid
)){
return
$result
;
}
$sql
=
'select * from comment where articleid = '
.
$articleid
;
global
$sqlHelper
;
$result
=
$sqlHelper
->get_all(
$sql
);
//p($result);
return
$result
;
}
/*
把评论数组数据转换为html格式
*/
function
commentArr2Html(
$comment_arr
) {
$str
=
''
;
if
(
is_array
(
$comment_arr
) && !
empty
(
$comment_arr
)){
$str
.=
'<div id="commentHolder">'
;
foreach
(
$comment_arr
as
$key
=>
$value
) {
$str
.=
'<div class="comment">'
;
$str
.=
'<p class="title"><a href="#">'
.
$value
[
'username'
] .
'</a>'
;
$str
.=
'<span>'
.
$value
[
'time'
] .
' 发表</span>'
;
$str
.=
'</p>'
;
global
$temp_arr
;
$temp_arr
=
array
();
//这里去查找当前评论下的所有引用的评论,并格式化为html字符串
$tmpStr
=
''
;
addCommentNode(
$comment_arr
,
$value
);
krsort(
$temp_arr
);
//根据key倒叙排序数组
$tmpStr
= getChildComment(
$temp_arr
);
//添加所有的引用评论
$str
.=
$tmpStr
;
$str
.=
"<p>"
.
$value
[
'content'
] .
"</p>"
;
$str
.=
'</div>'
;
}
$str
.=
'</div>'
;
}
return
$str
;
}
/*
把temp_arr数组中保存的引用评论信息转换为html形式
*/
function
getChildComment(
$temp_arr
){
$htmlStr
=
''
;
if
(!
is_array
(
$temp_arr
) ||
empty
(
$temp_arr
)){
return
''
;
}
foreach
(
$temp_arr
as
$value
){
$tmp
=
'<div>'
;
$tmp
.=
$htmlStr
;
$tmp
.=
'<span>'
.
$value
[
'username'
] .
' 原贴:</span><br />'
.
$value
[
'content'
];
$tmp
.=
'</div>'
;
$htmlStr
=
$tmp
;
}
return
$htmlStr
;
}
/*
list代表某一文章下的全部评论列表
cmt代表当前要显示的评论
*/
function
addCommentNode(
$list
,
$cmt
){
if
(isset(
$cmt
[
'pid'
]) &&
$cmt
[
'pid'
] !=
'0'
){
$find
= findParentCmt(
$list
,
$cmt
[
'pid'
]);
//找寻id等于当前评论的pid的评论,返回数组。
// 递归调用,只要pid不为零,就加入到引用评论列表
addCommentNode(
$list
,
$find
);
}
else
{
return
;
}
}
/**
* 查找list中找寻id等于pid的数组项,并返回
* @param [type] $list [description]
* @param [type] $cmtpid [description]
* @return [type] [description]
*/
function
findParentCmt(
$list
,
$cmtpid
){
foreach
(
$list
as
$key
=>
$value
) {
if
(
$value
[
'id'
] ==
$cmtpid
){
/* 用数组的方式来保存所有引用的评论 */
global
$temp_arr
;
$temp_arr
[] =
$list
[
$key
];
//p($list[$key]);echo "<hr>";
return
$list
[
$key
];
}
}
return
false;
}
$temp_arr
=
array
();
//设一个全局的数组变量来保存引用评论的信息
$list
= getCommentByArticleId();
//通过文章id获取所有的文章评论
$htmlStr
= commentArr2Html(
$list
);
//把获取到的评论格式化转换为html形式
?>
<form method=
"post"
action=
"comment.php"
name=
"comment_form"
id=
"comment_form"
>
<h1>文章评论的无限引用PHP+Mysql实现</h1><br /><br />
<!-- 这个是页面的结构 -->
<!--<div id=
"commentHolder"
>
<div
class
=
'comment'
>
<p
class
=
'title'
><a href=
"#"
>内蒙古网友</a>
<span>2008-3-24 16:33:49 发表</span>
</p>
<div>
//第四层引用
<div>
//第三层引用
<div>
//次顶层引用
<div>
//最顶层引用
<span>1 <a href=
"#"
>广州网友</a> 原贴:</span><br />
向马XX同志荣升台湾省省长表示祝贺!
</div>
<span>2 <a href=
"#"
>四川网友</a> 原贴:</span><br />
四川人民发来贺电!
</div>
<span>1 <a href=
"#"
>广州网友</a> 原贴:</span><br />
向马XX同志荣升台湾省省长表示祝贺!
</div>
<span>3 <a href=
"#"
>陕西西安网友</a> 原贴:</span><br />
陕西网友发来贺电
</div>
<p>内蒙网友发来贺电</p>
</div>
</div>-->
<!-- 直接
echo
输出拼好的html串 -->
<?php
echo
$htmlStr
; ?>
<br />
你的名字:<input type=
"text"
id=
""
name=
"name"
/><br />
联系方式或个人网站:<input type=
"text"
id=
""
name=
"site"
/><br />
选择引用的评论:
<select id=
""
name=
"commentid"
>
<!-- 这个可以查数据库获取 也可以写到上边的评论下加一个引用的链接 -->
<option value=
""
selected=
"selected"
>选择引用</option>
<option value=
"1"
>1</option>
<option value=
"2"
>2</option>
<option value=
"3"
>3</option>
<option value=
"4"
>4</option>
<option value=
"5"
>5</option>
<option value=
"6"
>6</option>
<option value=
"7"
>7</option>
<option value=
"8"
>8</option>
<option value=
"9"
>9</option>
</select><br />
评论内容酷站网软:<br />
<textarea id=
""
rows=
""
cols=
""
name=
"content"
></textarea>
<input type=
"hidden"
id=
""
name=
"articleid"
value=
"1"
/>
<input type=
"submit"
value=
"提交"
/><br />
</form>
</body>
</html>
|