0
我想实现一个使用php的线程评论系统,并且我发现了一些已经写好的东西,但是我不能确切地看到如何使用它,我对类不熟悉,所以我想知道是否有人可以帮忙解释我将如何使用代码。下面的代码是从网站有人可以解释一下这个类的php线程评论系统吗?
http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/
的代码的类如下:
class Threaded_comments
{
public $parents = array();
public $children = array();
/**
* @param array $comments
*/
function __construct($comments)
{
foreach ($comments as $comment)
{
if ($comment['parent_id'] === NULL)
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['parent_id']][] = $comment;
}
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
echo "\t";
}
echo $comment['text'];
echo "\n";
}
/**
* @param array $comment
* @param int $depth
*/
private function print_parent($comment, $depth = 0)
{
foreach ($comment as $c)
{
$this->format_comment($c, $depth);
if (isset($this->children[$c['id']]))
{
$this->print_parent($this->children[$c['id']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
该网站说,使用的一个例子是:
$comments = array( array('id'=>1, 'parent_id'=>NULL, 'text'=>'Parent'),
array('id'=>2, 'parent_id'=>1, 'text'=>'Child'),
array('id'=>3, 'parent_id'=>2, 'text'=>'Child Third level'),
array('id'=>4, 'parent_id'=>NULL, 'text'=>'Second Parent'),
array('id'=>5, 'parent_id'=>4, 'text'=>'Second Child')
);
$threaded_comments = new Threaded_comments($comments);
$threaded_comments->print_comments();
但这是我遇到问题的地方。首先,我不完全相信,我应该如何设置数据库,
目前它只有3排,
id
page
user
comment
,我会查询使用mysqli的预处理语句这个数据库。大概是这样的:
$DBH = getDBH();
$q = $DBH->prepare("SELECT * FROM comments WHERE page = ?");
$q->bind_param("i", $page);
$q->execute();
,但我不知道,我怎么能去展示这一点,我知道需要有另一行添加到数据库中,声明如果评论是另一个评论的孩子。任何帮助是极大的赞赏
我建议你首先阅读本教程:http://dev.mysql.com/tech-resources/articles/hierarchical-data.html这是如何使用mysql存储分层数据的一个很好的概述。你可以跳过嵌套的东西,它可能不适用于线程注释。 – GWW 2011-01-12 20:26:59
非常感谢您的帮助。 Althoug我要求一个探索,我也在寻找关于这个问题的一些信息。非常感谢帮忙。 – mcbeav 2011-01-12 21:26:59