Threaded array library for Codeigniter

This library take an array of comments (can also be anything with ids and parent_ids)

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class threaded
{

    public $parents  = array();
    public $children = array();

    /**
     * @param array $comments
     */
    public function arrange($comments)
    {
        foreach ($comments as $comment)
        {
            if ($comment['parent_id'] === NULL)
            {
                $this->parents[$comment['id']][] = $comment;
            }
            else
            {
                $this->children[$comment['parent_id']][] = $comment;
            }
        }
        $this->print_comments();
    }

    private function tabulate($depth)
    {
        for ($depth; $depth > 0; $depth--)
        {
            echo "t";
        }
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function format_comment($comment, $depth)
    {

        echo "n";

        $this->tabulate($depth+1);

        echo "<li>";
        echo $comment['text'];
        echo "</li>n";
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function print_parent($comment, $depth = 0)
    {
        $this->tabulate($depth);
        echo "<ul>";
        foreach ($comment as $c)
        {
            $this->format_comment($c, $depth);

            if (isset($this->children[$c['id']]))
            {
                $this->print_parent($this->children[$c['id']], $depth + 1);
            }
        }
        $this->tabulate($depth);
        echo "</ul>n";
    }

    private function print_comments()
    {
        foreach ($this->parents as $c)
        {
            $this->print_parent($c);
        }
    }

}

To use the library, put it in “libraries” and load in codeigniter with

$this->load->library('threaded');

and name it threaded.php

This library is based on the following post, http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/

So the array we would use is the same as the above blog post,

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

and you process the array into unordered lists like this.

$this->threaded->arrange($comments);

This is the result you should get in html

<ul>
    <li>Parent</li>
    <ul>
        <li>Child</li>
        <ul>
            <li>Child Third level</li>
        </ul>
    </ul>
</ul>
<ul>
    <li>Second Parent</li>
    <ul>
        <li>Second Child</li>
    </ul>
</ul>
This entry was posted in Uncategorized by admin. Bookmark the permalink.

One thought on “Threaded array library for Codeigniter