1

I found a pre-written script of a class to create threaded comments, but after trying to implement it, nothing prints. The array 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();
mcbeav
  • 11,893
  • 19
  • 54
  • 84
  • related question: http://stackoverflow.com/questions/4673621/can-someone-hello-explain-this-class-for-a-php-threaded-comments-system – Trufa Jan 13 '11 at 03:44

1 Answers1

2

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.

Naatan
  • 3,424
  • 4
  • 32
  • 51
  • Yep, when i var_dump($all_results) it displays the array, so what am i doing wrong here? – mcbeav Jan 13 '11 at 04:56
  • 1
    Would help if you could show the result of the var_dump. I tried it with my own array and it worked. – Naatan Jan 13 '11 at 14:06
  • array(2) { [0]=> array(3) { ["id"]=> int(1) ["parent_id"]=> int(0) ["text"]=> string(32) "test comment one, parent comment" } [1]=> array(3) { ["id"]=> int(2) ["parent_id"]=> int(0) ["text"]=> string(30) "test comment 2 child comment 1" } } – mcbeav Jan 13 '11 at 22:14
  • thanks for keeping in touch and trying to help me out, i really appreciate this. – mcbeav Jan 13 '11 at 22:15
  • 1
    Change your fetch method to $q->fetch(PDO::FETCH_ASSOC) – Naatan Jan 14 '11 at 02:44
  • im using mysqli prepared statments, not PDO statements, will that work? Also, i would prefer not to do this because PDO does not support SSL, so if someone views the page under a secure connection, it wouldnt work. – mcbeav Jan 14 '11 at 03:07
  • its just shooting me an error, stating fetch() expects 0 parameters, btu it is given one, that would be (PDO::FETCH_ASSOC), as far as i know you cant mix PDO and mysqli prepared, but i dont know 100% for sure. – mcbeav Jan 14 '11 at 03:09
  • 1
    My bad, anyway your problem is not in the mysql query. Your code is checking for parent === null whilst the MySQL code is returning 0, not NULL. So just change "if ($comment['parent_id'] === NULL)" to "if ($comment['parent_id'] === 0)" – Naatan Jan 14 '11 at 03:44
  • man, you are a serious life saver. thanks so much! i can't thank you enough! thanks again for all of your time and effort! – mcbeav Jan 14 '11 at 04:31