Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

PHP Threaded Comment Class

A few years ago I built a dodgy little site at Usenet.com.au that rendered Australian text Usenet newsgroups. While the front-end is offline, it'll be back online sometime in the future. One of the biggest challenges I faced in the early days was orientated around optimizing threaded articles. With several million articles in the database it quickly became evident that the original method of storing and displaying articles was problematic. It was slow, queries were long and complicated, and CPU resources were quite high.

My solution (for a small satellite website) came in the way of a simple class written by Jon Gales of JonGales.com . I've modified Jon's code myself and it works great. There are a lot of threaded comment classes around the place (including one within WordPress available for use under the GPL) but I found that Jon's was simply easy to use and implement for my quick and dirty solution.

The code is designed to be implemented into your own PHP project.

John's original code is below.

1
<?php 
2
class Threaded_comments
3
{
4
    public $parents  = array();
5
    public $children = array();
6
 
7
    /**
8
     * @param array $comments
9
     */
10
    function __construct($comments)
11
    {
12
        foreach ($comments as $comment)
13
        {
14
            if ($comment['parent_id'] === NULL)
15
            {
16
                $this->parents[$comment['id']][] = $comment;
17
            }
18
            else
19
            {
20
                $this->children[$comment['parent_id']][] = $comment;
21
            }
22
        }
23
    }
24
 
25
    /**
26
     * @param array $comment
27
     * @param int $depth
28
     */
29
    private function format_comment($comment, $depth)
30
    {
31
        for ($depth; $depth > 0; $depth--)
32
        {
33
            echo "\t";
34
        }
35
 
36
        echo $comment['text'];
37
        echo "\n";
38
    }
39
 
40
    /**
41
     * @param array $comment
42
     * @param int $depth
43
     */
44
    private function print_parent($comment, $depth = 0)
45
    {
46
        foreach ($comment as $c)
47
        {
48
            $this->format_comment($c, $depth);
49
 
50
            if (isset($this->children[$c['id']]))
51
            {
52
                $this->print_parent($this->children[$c['id']], $depth + 1);
53
            }
54
        }
55
    }
56
 
57
    public function print_comments()
58
    {
59
        foreach ($this->parents as $c)
60
        {
61
            $this->print_parent($c);
62
        }
63
    }
64
 
65
}

Here's example usage with the comment data provided as an array.

1
$comments = array(  array('id'=>1, 'parent_id'=>NULL,   'text'=>'Parent'),
2
                    array('id'=>2, 'parent_id'=>1,      'text'=>'Child'),
3
                    array('id'=>3, 'parent_id'=>2,      'text'=>'Child Third level'),
4
                    array('id'=>4, 'parent_id'=>NULL,   'text'=>'Second Parent'),
5
                    array('id'=>5, 'parent_id'=>4,   'text'=>'Second Child')
6
                );
7
 
8
$threaded_comments = new Threaded_comments($comments);
9
$threaded_comments->print_comments();

Example output:

1
Parent
2
    Child
3
        Child Third level
4
Second Parent
5
    Second Child

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment