Php generic tree classes can generate any tree structure
Php generic tree classes can generate any tree structure
Class tree
{
/**
* The two-dimensional array required to generate a Tree Structure
* @ Var array
*/
Var $ arr = array ();
/**
* The modifier required to generate a tree structure, which can be replaced by an image.
* @ Var array
*/
Var $ icon = array ('│', 'hangzhou', 'hangzhou ');
/**
* @ Access private
*/
Var $ ret = '';
/**
* Constructor, initialization class
* @ Param array 2-dimensional array, for example:
* Array (
* 1 => array ('id' => '1', 'parentid' => 0, 'name' => 'level 1 topic 1 '),
* 2 => array ('id' => '2', 'parentid' => 0, 'name' => 'level 1 column 2 '),
* 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level column 1 '),
* 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '),
* 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '),
* 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'level 3 topic 1 '),
* 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-level column 2 ')
*)
*/
Function tree ($ arr = array ())
{
$ This-> arr = $ arr;
$ This-> ret = '';
Return is_array ($ arr );
}
/**
* Obtain the parent array.
* @ Param int
* @ Return array
*/
Function get_parent ($ myid)
{
$ Newarr = array ();
If (! Isset ($ this-> arr [$ myid]) return false;
$ Pid = $ this-> arr [$ myid] ['parentid'];
$ Pid = $ this-> arr [$ pid] ['parentid'];
If (is_array ($ this-> arr ))
{
Foreach ($ this-> arr as $ id => $)
{
If ($ a ['parentid'] = $ pid) $ newarr [$ id] = $;
}
}
Return $ newarr;
}
/**
* Obtain the child-level array.
* @ Param int
* @ Return array
*/
Function get_child ($ myid)
{
$ A = $ newarr = array ();
If (is_array ($ this-> arr ))
{
Foreach ($ this-> arr as $ id => $)
{
If ($ a ['parentid'] ==$ myid) $ newarr [$ id] = $;
}
}
Return $ newarr? $ Newarr: false;
}
/**
* Obtain the array of the current position.
* @ Param int
* @ Return array
*/
Function get_pos ($ myid, & $ newarr)
{
$ A = array ();
If (! Isset ($ this-> arr [$ myid]) return false;
$ Newarr [] = $ this-> arr [$ myid];
$ Pid = $ this-> arr [$ myid] ['parentid'];
If (isset ($ this-> arr [$ pid])
{
$ This-> get_pos ($ pid, $ newarr );
}
If (is_array ($ newarr ))
{
Krsort ($ newarr );
Foreach ($ newarr as $ v)
{
$ A [$ v ['id'] = $ v;
}
}
Return $;
}
/**
* Tree Structure
* @ Param int ID, which indicates that all sub-levels under this ID are obtained.
* @ Param string: the basic code for generating the tree structure. For example, "<option value = $ id $ selected> $ spacer $ name </option>"
* @ Param int indicates the selected ID, for example, used in tree-type drop-down boxes.
* @ Return string
*/
Function get_tree ($ myid, $ str, $ sid = 0, $ adds = '', $ str_group = '')
{
$ Number = 1;
$ Child = $ this-> get_child ($ myid );
If (is_array ($ child ))
{
$ Total = count ($ child );
Foreach ($ child as $ id => $)
{
$ J = $ k = '';
If ($ number = $ total)
{
$ J. = $ this-> icon [2];
}
Else
{
$ J. = $ this-> icon [1];
$ K = $ adds? $ This-> icon [0]: '';
}
$ Spacer = $ adds? $ Adds. $ j :'';
$ Selected = $ id = $ sid? 'Selected ':'';
@ Extract ($ );
$ Parentid = 0 & $ str_group? Eval ("$ nstr =" $ str_group ";"): eval ("$ nstr =" $ str ";");
$ This-> ret. = $ nstr;
$ This-> get_tree ($ id, $ str, $ sid, $ adds. $ k. '& nbsp;', $ str_group );
$ Number ++;
}
}
Return $ this-> ret;
}
/**
* Similar to the previous method, but multiple options are allowed.
*/
Function get_tree_multi ($ myid, $ str, $ sid = 0, $ adds = '')
{
$ Number = 1;
$ Child = $ this-> get_child ($ myid );
If (is_array ($ child ))
{
$ Total = count ($ child );
Foreach ($ child as $ id => $)
{
$ J = $ k = '';
If ($ number = $ total)
{
$ J. = $ this-> icon [2];
}
Else
{
$ J. = $ this-> icon [1];
$ K = $ adds? $ This-> icon [0]: '';
}
$ Spacer = $ adds? $ Adds. $ j :'';
$ Selected = $ this-> have ($ sid, $ id )? 'Selected ':'';
// Echo $ sid. '=>'. $ id. ':'. $ selected. '. <br/> ';
@ Extract ($ );
Eval ("$ nstr =" $ str ";");
$ This-> ret. = $ nstr;
$ This-> get_tree_multi ($ id, $ str, $ sid, $ adds. $ k. '& nbsp ;');
$ Number ++;
}
}
Return $ this-> ret;
}
Function have ($ list, $ item ){
Return (strpos (','. $ list. ','. $ item .','));
}
}