Hierarchical Category Management (Project Management Tools)

Hierarchical Category Management (Project Management Tools)

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 an bonus you get a Database class which makes use of PHP’s PDO for communication with the database. You can easily get rid of it and implement your own database class.

Example #1:

The following code
$root = Mptt::factory()->create_root('Root node'); // $root stores the id of the new 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);

...will generate a data structure similar to the one on the screenshot bellow:
Screenshot of Data structure

Example #2

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

Screenshot of generated html select box

You can than print the path with a simple loop.

Example #3

// Calling get_path will generate a path to the desired node
echo Mptt::factory()->get_path(7);
Array
(
    [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
        )
)
Here is a complete list of functions and their description: <tbody> </tbody>
as_array() Returns raw array
as_html() Creates a multi-level menu compatible with the Bootstrap css framework
as_select() Creates a HTML form select element of the whole tree
create_child() Creates a new child node of the node with the given id
create_root() Creates a root node
create_sibling() Creates a new sibling next to the given node
delete_node() 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() Gets an object with all data of a node
get_path() Gets a array containing the path to defined node
is_descendant() Checks whether the node is a descendant of another
is_leaf() Checks whether the node is leaf or not (A node is leaf when it has no child nodes)

Questions

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



CodeCanyon new PHP Scripts items

Related Posts