7th
MAY

HTree Hierarchial Model - a CodeIgniter Model

Posted by David under CodeIgniter Libraries

We needed a class that would retrieve a block of data with parent/child relationships from mySQL and store it in an array which correctly represents the three dimensional nature of the parent/child relationships. The class needed to also have methods for add, deleting and moving entries within and between categories.

What is the model for ?

Essentially we want to turn this:

PK  parent  name
-------------------------
1   0       Animals
2   1       Cat

Into this:

Array
(
    [0] => Array
        (
         [PK] => 1
         [parent] => 0
         [name] => Animals
         [childs] => Array
                        (
                        [0] => Array
                            (
                             [PK] => 2
                             [parent] => 1
                             [name] => Cat
                        )
        )
)

What can it do ?

The model contains simple functions to manage the data items such as:

$this->HModel->add_item("test item", 3);
$this->HModel->delete_item(53);
$this->HModel->move_item_down(14);
$this->HModel->is_item_live(21);

How do I use it ?

Simply copy the hierarchial_model.php file into the system/application/models folder within your CodeIgniter installation to complete the installation. Complete documentation is available in the HTree Hierarchial Model Guide.

Reader's Comments

  • Thank you for the useful library. I was in need of something like that.

    I am so embarrassed to say that cause I am a programmer too :) but I don’t have the time to write a library on my own.

    Thanx again !

    See ya.

Leave a Reply

(required)
(will not be published) (required)

The Lab is the web development blog for PX Webdesign that covers many subjects from PHP, CSS, jQuery, CodeIgniter, Design and XHTML / HTML