有人可以为php线程注释系统解释此类吗? [英] Can Someone hello explain this class for a php threaded comments system?
问题描述
我正在尝试使用php实现线程注释系统,但我发现已经编写了一些东西,但是我无法完全看到如何使用它,我对类一点都不熟悉,所以我想知道是否有人可以提供帮助解释我将如何使用代码.下面的代码来自网站
I am trying to implement a threaded comment system using php, and i found something already written, but i can not exactly see how to use it, i am not familiar at all with classes, so i was wondering if someone could help explain how i would use the code. the code below is from the website
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);
}
}
}
该网站说一个用法示例是:
The site says an example of usage would be:
$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();
但是这里是我遇到问题的地方.首先,我不确定我应该如何设置数据库,
but here is where I am having problems. First off, I am not exactly sure how i should be setting up the database,
目前只有3行,
id
page
user
comment
我将使用mysqli准备的语句查询该数据库.大概是这样的:
And i will be querying this database using mysqli prepared statements. Probably something like this:
$DBH = getDBH();
$q = $DBH->prepare("SELECT * FROM comments WHERE page = ?");
$q->bind_param("i", $page);
$q->execute();
但是我不确定,如何显示此信息,我知道需要在数据库中添加另一行,以声明该注释是否为另一注释的子项.任何帮助将不胜感激
but I am not sure, how i could go about displaying this, i know there needs to be another row added to the database, to declare if the comment is a child of another comment. Any help is greatly appreciated
推荐答案
您需要向表中添加另一列parent_id
You would need to add another column to your table, parent_id
然后,您像往常一样获取所有注释,将它们放入数组中,然后将其传递给Threaded_comments
构造函数
Then you fetch all comments like usual, put them into an array and pass it to Threaded_comments
constructor
$result = $mysqli->query(
"SELECT id, parent_id, comment AS text
FROM yourtable");
$all_results = $result->fetch_all(MYSQLI_ASSOC);
/* For MySQLi_STMT */
$q = $DBH->prepare("SELECT id, parent_id, comment FROM comments WHERE page = ?");
$q->bind_param("i", $page);
$q->execute();
$q->bind_result($id, $parent_id, $comment);
$all_results = array();
while ($q->fetch()) {
$all_results[] = array(
'id' => $id,
'parent_id' => $parent_id,
'text' => $comment);
}
$q->close();
$tc = new Threaded_Comments($all_results);
这篇关于有人可以为php线程注释系统解释此类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!