Skip to main content

Hierarchical Category Management Download

Hierarchical Category Management Download




Hierarchical Category Management





Price: $10








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: Creating nodes
The following code
// $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);


Please analyze the code carefully…it will generate a data structure similar to the one on the screenshot bellow:




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
$root = 1;
echo Mptt::factory($root)->as_select('node_id');




Example #3: Getting the path of a node
// Calling get_path will generate a path to the desired node
echo Mptt::factory()->get_path(7);

You can than print the path with a simple loop.

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
)
)

Example #4: Deleting
To delete a node and it’s chidlren 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'];

Mptt::factory()->delete_id($node_id); ?>

Example #4: Check if node is leaf
A leaf node is a node which has no child nodes. Checking this factor can be usefull if you want the user to specify exactly what’s requested from him.
// 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
}
else
{
// it has child nodes, show an error message
}

Example #4: Configuration
There is a number of things you can do to make the returned list more suited for your purposes. Please 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 usefull 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()
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.


Comments