开发者

Can anyone help me with php threaded comments?

I found a pre-written script of a class to create threaded comments, but after trying to implement it, nothing prints. The array 开发者_StackOverflow中文版si holding data, and I have confirmed that, but, nothing will print when the function is called, so I was wondering if someone could please help.

the script can be found here:

It is also below 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 code i have used with help from germannrumm is as follows:

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

$all_results = array();

while ($q->fetch()) {
    $all_results[] = array(
        'id' => $id, 
        'parent_id' => $parent_id, 
        'text' => $text);
}
$q->close();

any help is greatly appreciated! thanks!

$tc = new Threaded_comments($all_results);
$tc->print_comments();


The class is working properly, but we can't test your database code for you. Make sure that you are indeed passing an array to the class. Do a var_dump($all_results) right before you initiate the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜