Php unlimited classification tree class code _ PHP Tutorial

Source: Internet
Author: User
Php tree code with unlimited classification. Copy the code as follows :? Php ** bylenush; * classTree {var $ dataarray (); var $ childarray (-1 array (); var $ layerarray (-1-1 ); var $ parentarray (); functionTree The code is as follows:


/**
By lenush;
*/
Class Tree
{
Var $ data = array ();
Var $ child = array (-1 => array ());
Var $ layer = array (-1 =>-1 );
Var $ parent = array ();

Function Tree ($ value)
{
$ This-> setNode (0,-1, $ value );
} // End func

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

Function getList (& $ tree, $ root = 0)
{
Foreach ($ this-> child [$ root] as $ key => $ id)
{
$ Tree [] = $ id;

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


Function getValue ($ id)
{
Return $ this-> data [$ id];
} // End func


Function getLayer ($ id, $ space = false)
{
Return $ space? Str_repeat ($ space, $ this-> layer [$ id]): $ this-> layer [$ id];
} // End func

Function getParent ($ id)
{
Return $ this-> parent [$ id];
} // End func

Function getParents ($ id)
{
While ($ this-> parent [$ id]! =-1)
{
$ Id = $ parent [$ this-> layer [$ id] = $ this-> parent [$ id];
}

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

Return $ parent;
} // End func

Function getChild ($ id)
{
Return $ this-> child [$ id];
} // End func

Function getChilds ($ id = 0)
{
$ Child = array ($ id );
$ This-> getList ($ child, $ id );

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


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

// SetNode (Directory ID, parent ID, directory name );
$ Tree-> setNode (1, 0, 'directory 1 ');
$ Tree-> setNode (2, 1, '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 )."
\ N ";
}
?>

The http://www.bkjia.com/PHPjc/320977.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320977.htmlTechArticle code is as follows :? Php/** by lenush; */class Tree {var $ data = array (); var $ child = array (-1 = array ()); var $ layer = array (-1 =-1); var $ parent = array (); function Tree...

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.