Nested comments code not working
<p>I'm trying to implement code for nested commenting which I found at <a href="http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/" rel="nofollow">http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/</a>.</p>

<p>But for some reason I can't get it to work. My result is
"Parent Child Child Third level Second Parent Second Child" in one row
without any nesting. What am I doing wrong? Is there maybe a setting or extension which I have to enable for this to work? Thanks in advance!</p>

<p>Here's the exact code I'm using:</p>

<pre><code>&lt;?php
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-&gt;parents[$comment['id']][] = $comment;
        }
        else
        {
            $this-&gt;children[$comment['parent_id']][] = $comment;
        }
    }
}

/**
 * @param array $comment
 * @param int $depth
 */
private function format_comment($comment, $depth)
{
    for ($depth; $depth &gt; 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-&gt;format_comment($c, $depth);

        if (isset($this-&gt;children[$c['id']]))
        {
            $this-&gt;print_parent($this-&gt;children[$c['id']], $depth + 1);
        }
    }
}

public function print_comments()
{
    foreach ($this-&gt;parents as $c)
    {
        $this-&gt;print_parent($c);
    }
}

}



$comments = array(  array('id'=&gt;1, 'parent_id'=&gt;NULL,   'text'=&gt;'Parent'),
                array('id'=&gt;2, 'parent_id'=&gt;1,      'text'=&gt;'Child'),
                array('id'=&gt;3, 'parent_id'=&gt;2,      'text'=&gt;'Child Third level'),
                array('id'=&gt;4, 'parent_id'=&gt;NULL,   'text'=&gt;'Second Parent'),
                array('id'=&gt;5, 'parent_id'=&gt;4,   'text'=&gt;'Second Child')
            );

$threaded_comments = new Threaded_comments($comments);

$threaded_comments-&gt;print_comments();

?&gt;
</code></pre>
