PHP unlimited classification class

Source: Internet
Author: User

During this time, I was writing a Department permission system and needed to use the unlimited classification technology. I found a lot of code about classification and collected them one by one.
<? Php
/**
* @ Author YangHuan
* @ Datetime
* @ Version 1.0.0
*/

/**
* Short description.
*
* Detail description
* @ Author
* @ Version 1.0
* @ Copyright
* @ Access public
*/
Class Tree
{
/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ data = array ();

/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ child = array (-1 => array ());

/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ layer = array (-1 =>-1 );

/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ parent = array ();

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function Tree ($ value)
{
$ This-> setNode (0,-1, $ value );
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function setNode ($ id, $ parent, $ value)
{
$ Parent = $ parent? $ Parent: 0;

$ This-> data [$ id] = $ value;
$ This-> child [$ id] = array ();
$ This-> child [$ parent] [] = $ id;
$ This-> parent [$ id] = $ parent;

If (! Isset ($ this-> layer [$ parent])
{
$ This-> layer [$ id] = 0;
}
Else
{
$ This-> layer [$ id] = $ this-> layer [$ parent] + 1;
}
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getList (& $ tree, $ root = 0)
{
Foreach ($ this-> child [$ root] as $ key => $ id)
{
$ Tree [] = $ id;

If ($ this-> child [$ id]) $ this-> getList ($ tree, $ id );
}
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getValue ($ id)
{
Return $ this-> data [$ id];
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getLayer ($ id, $ space = false)
{
Return $ space? Str_repeat ($ space, $ this-> layer [$ id]): $ this-> layer [$ id];
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getParent ($ id)
{
Return $ this-> parent [$ id];
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getParents ($ id)
{
While ($ this-> parent [$ id]! =-1)
{
$ Id = $ parent [$ this-> layer [$ id] = $ this-> parent [$ id];
}

Ksort ($ parent );
Reset ($ parent );

Return $ parent;
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getChild ($ id)
{
Return $ this-> child [$ id];
} // End func

/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getChilds ($ id = 0)
{
$ Child = array ($ id );
$ This-> getList ($ child, $ id );

Return $ child;
} // End func
} // End class

?>

--------------------------------------------------------------------------------

Usage

PHP code :--------------------------------------------------------------------------------
// New Tree (root directory name );
// The ID of the root directory is automatically allocated to 0.
$ Tree = new Tree ('root directory ');

// SetNode (directory ID, parent ID, directory name );
$ Tree-> setNode (1, 0, 'Directory 1 ');
$ Tree-> setNode (2, 0, 'Directory 2 ');
$ Tree-> setNode (3, 0, 'Directory 3 ');
$ Tree-> setNode (4, 3, 'Directory 100 ');
$ Tree-> setNode (5, 3, 'Directory 100 ');
$ Tree-> setNode (6, 3, 'Directory 100 ');
$ Tree-> setNode (7, 2, 'Directory 100 ');
$ Tree-> setNode (8, 2, 'Directory 100 ');
$ Tree-> setNode (9, 2, 'Directory 100 ');
$ Tree-> setNode (10, 6, 'Directory 3.3.1 ');
$ Tree-> setNode (11, 6, 'Directory 3.3.2 ');
$ Tree-> setNode (12, 6, 'Directory 3.3.3 ');

// GetChilds (specify the directory ID );
// Obtain the sub-directory of the specified directory. If no sub-directory is specified, it starts from the root directory.
$ Category = $ Tree-> getChilds ();

// Print the output
Foreach ($ category as $ key => $ id)
{
Echo $ Tree-> getLayer ($ id, '|-'). $ Tree-> getValue ($ id). "<br> \ n ";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.