Hierarchical Category Management

Hierarchical Category Management

Follow me on CodeCanyon Follow me on Twitter

What is it?

Nested sets or Nested Set Model is a way to effectively store hierarchical data in a relational table. The main features are:

  • Generation of select elements for forms
  • Generation of lists (ul) consistent with Twitters Bootstrap framework
  • Generation of raw nested (multidimensional) arrays so everyone can extend the script if needed without much trouble

As a bonus you get a Database helper which makes use of PHP’s PDO for communication with the database. You can easily get rid of it and use your own database class.

Example #1: Creating nodes

// $root stores the id of the new node
$root =  Mptt::factory()->create_root('Root node');
             Mptt::factory()->create_child('Child node 1', $root);
             Mptt::factory()->create_child('Child node 2', $root);
$child = Mptt::factory()->create_child('Child node 3', $root);
             Mptt::factory()->create_child('Child node 4', $root);
             Mptt::factory()->create_child('Child node 5', $root);
                 $sibling = Mptt::factory()->create_child('Child of child', $child);
                 Mptt::factory()->create_sibling('Sibling of child', $sibling);

Analyze the code carefully…it will generate an array similar to the one on the screenshot bellow:

Screenshot of array

Example #2: Generating form elements

To make a select element simply pass the id of the desired node to the factory method and then call as_select() on the returned object.

// Calling as_select will generate a valid select element
$node_id = 1;
echo Mptt::factory($node_id)->as_select('node_id');

Screenshot of generated html select box

Example #3: Getting the path of a node

$node_id = 7;
print_r( Mptt::factory()->get_path($node_id) );

Calling get_path will generate an array containing all parent nodes as well as the node itself.

    [1] => Array
            [id] => 1
            [title] => Root node
            [lft] => 1
            [rgt] => 16

    [4] => Array
            [id] => 4
            [title] => Child node 3
            [lft] => 6
            [rgt] => 11

    [7] => Array
            [id] => 7
            [title] => Child of child
            [lft] => 7
            [rgt] => 8

You can then print the path with a simple loop.

Example #4: Deleting

To delete a node and it’s children simply call the delete method and pass the id of the node you want to delete

// don't forget to do some validation after you get the id
$node_id = $_POST['node_id'];

Example #4: Check if node is leaf

A leaf node is a node which has no children nodes. Checking this factor can be useful in situations where you want the user to select the most specific item there is in a given set.

// don't forget to do some validation after you get the id
$node_id = $_POST['node_id'];

if( Mptt::factory()->is_leaf($node_id) )
    // it is leaf ... do whatever you want
    // it has children nodes, show an error message

Example #4: Configuration

There is a number of things you can do to make the returned list more suitable for your needs. Take a look at the config parameters bellow:

echo Mptt::factory(array('only_leaf' => TRUE, 'limit' => 5))->as_select('node_id');

List of configuration parameters, their description, default and expected values

key default type description
only_leaf false boolean (true/false) Select only leaf nodes from the database?
only_leaf_select false boolean (true/false) Make nodes with children unselectable when using as_select()?
limit NULL integer Limit the number of returned nodes (only useful in combination with the only_leaf parameter)
indenter ` `(four spaces) string The bit of text that makes depth of the node visible in select elements
prefix_leaf ⊢ string Sign that is prepended to the title of leaf items
prefix_non_leaf ∟ string Sign that is prepended to the title of non-leaf items

Complete list of functions and their description

as_array($nested = TRUE) Returns raw array
as_html($items = NULL, array $attributes = NULL, $base_url = ’/search?cid=’) Creates a multi-level menu compliant with the Bootstrap css framework
as_select($name, $selected = NULL, array $attributes = NULL) Creates a HTML form select element of the whole tree
create_child($name, $parent_id) Creates new node as child of $parent_id
create_root($name) Creates root node
create_sibling($name, $sibling_id) Creates new node as sibling of $sibling_id
delete_node($node_id) Deletes a node and all it’s children
factory() Sets config and returns Mptt object
get_items() Gets items depending on provided criteria and stores them in a local array
get_node($node_id) Gets all data associated with a node
get_path() Gets an array containing all parent nodes as well as the node itself
is_descendant($node_id, $parent_node_id) Checks whether a node is a descendant of another
is_leaf($node_id) Checks whether the node is leaf or not (A node is leaf when it has no child nodes)


If you have any questions before buying, please don’t hesitate to send me contact me.