Ec (2); php unlimited paging code classTree {& nbsp; public $ dataarray (); & nbsp; & nbsp; public $ cateArrayarray (); & nbsp; & script ec (2); script
Php unlimited paging code
Class Tree
{
Public $ data = array ();
Public $ cateArray = array ();
Function Tree ()
{
}
Function setNode ($ id, $ parent, $ value)
{
$ Parent = $ parent? $ Parent: 0;
$ This-> data [$ id] = $ value;
$ This-> cateArray [$ id] = $ parent;
}
Function getChildsTree ($ id = 0)
{
$ Childs = array ();
Foreach ($ this-> cateArray as $ child => $ parent)
{
If ($ parent = $ id)
{
$ Childs [$ child] = $ this-> getChildsTree ($ child );
}
}
Return $ childs;
}
Function getChilds ($ id = 0)
{
$ ChildArray = array ();
$ Childs = $ this-> getChild ($ id );
Foreach ($ childs as $ child)
{
$ ChildArray [] = $ child;
$ ChildArray = array_merge ($ childArray, $ this-> getChilds ($ child ));
}
Return $ childArray;
}
Function getChild ($ id)
{
$ Childs = array ();
Foreach ($ this-> cateArray as $ child => $ parent)
{
If ($ parent = $ id)
{
$ Childs [$ child] = $ child;
}
}
Return $ childs;
}
// Obtain the parent node in a single line
Function getNodeLever ($ id)
{
$ Parents = array ();
If (key_exists ($ this-> cateArray [$ id], $ this-> cateArray ))
{
$ Parents [] = $ this-> cateArray [$ id];
$ Parents = array_merge ($ parents, $ this-> getNodeLever ($ this-> cateArray [$ id]);
}
Return $ parents;
}
Function getLayer ($ id, $ preStr = '| -')
{
Return str_repeat ($ preStr, count ($ this-> getNodeLever ($ id )));
}
Function getValue ($ id)
{
Return $ this-> data [$ id];
} // End func
}
/* $ Tree = new Tree ("select a category ");
// SetNode (directory ID, parent ID, directory name );
$ Tree-> setNode (1, 0, 'Directory 1 ');
$ Tree-> setNode (2, 1, 'Directory 2 ');
$ Tree-> setNode (5, 3, 'Directory 5 ');
$ Tree-> setNode (3, 0, 'Directory 3 ');
$ Tree-> setNode (4, 2, 'Directory 4 ');
$ Tree-> setNode (9, 4, 'Directory 9 ');
$ Tree-> setNode (6, 2, 'Directory 6 ');
$ Tree-> setNode (7, 2, 'Directory 7 ');
$ Tree-> setNode (8, 3, 'Directory 8 ');
// Print_r ($ Tree-> getChildsTree (0 ));
// Print_r ($ Tree-> getChild (0 ));
// Print_r ($ Tree-> getLayer (2 ));
$ Category = $ Tree-> getChilds ();
// Print the output
Foreach ($ category as $ key => $ id)
{
Echo $ id. $ Tree-> getLayer ($ id, '|-'). $ Tree-> getValue ($ id). "\ n ";
}*/
?>