【问题标题】:Can Someone hello explain this class for a php threaded comments system?有人可以帮助解释这个类的 php 线程评论系统吗?
【发布时间】:2011-01-12 20:20:19
【问题描述】:

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

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

【问题讨论】:

  • 我建议您从阅读本教程开始:dev.mysql.com/tech-resources/articles/hierarchical-data.html 这是关于如何使用 mysql 存储分层数据的一个很好的大纲。您可以跳过可能不适用于线程化 cmets 的嵌套集合内容。
  • 非常感谢您的帮助。尽管我要求解释,但我也在寻找有关该主题的一些信息。非常感谢您的帮助。

标签: php mysqli prepared-statement threaded-comments


【解决方案1】:

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

然后像往常一样获取所有 cmets,将它们放入一个数组并传递给 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);

【讨论】:

  • 调用未定义的方法 mysqli_stmt::fetch_all() 是我收到的错误,关于如何修复它的任何想法?如果我使用准备好的语句,如何将结果传递给数组?
  • 是的,我现在对此有点困惑,我一直收到一个错误,我确定这是我的责任,但我希望你能在这里帮助我。
  • 列名已重命名为文本,我已尝试添加您提供的行,但我没有运气,我不确定这里有什么问题。
  • 我更新了我的答案,包括 MySQLi 两种方式的获取。不过,我对 MySQLi 不太擅长
  • 感谢更新和帮助,我不再收到任何错误,但现在什么都没有显示,我现在要搞砸了,我想我应该能够让它工作.
猜你喜欢
  • 1970-01-01
  • 2018-07-23
  • 2020-01-31
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2011-04-26
相关资源
最近更新 更多