Can Someone hello explain this class for a php thr

2019-09-18 11:21发布

问题:

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

回答1:

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