有人可以解释一下 php 线程评论系统的这个类吗?

发布于 2024-10-11 13:35:42 字数 2647 浏览 0 评论 0原文

我正在尝试使用 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();

但我不确定,我如何才能显示它,我知道需要在数据库中添加另一行,以声明该评论是否是另一个评论的子级。非常感谢任何帮助

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/

the code for the classes is as follows:

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,

Currently it has just 3 rows,

id
page
user
comment

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-10-18 13:35:42

您需要在表中添加另一列 parent_id

然后像往常一样获取所有评论,将它们放入数组中并将其传递给 Threaded_comments 构造函数

$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);

You would need to add another column to your table, parent_id

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文