Php unlimited classification and support for output tree chart details _ PHP Tutorial

Source: Internet
Author: User
Php supports unlimited classification and provides a detailed description of the output tree chart. Copy the code as follows :? Php *** Common tree class, which can generate any tree structure * classtree {*** the two-dimensional array required to generate the tree structure * @ vararray * var $ arr The code is as follows:


/**
* A common tree class 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
*-------------------------------------
* @ Author Midnight (Yang Yunzhou), yangyunzhou@foxmail.com
* @ Param $ myid indicates that all sub-levels under this ID are obtained.
* @ Param $ str generate the basic code of the tree structure, for example :"\ $ Spacer \ $ name"
* @ Param $ ID the selected ID. for example, you need to use this ID when creating a tree drop-down box.
* @ Param $ adds
* @ Param $ str_group
* @ Return unknown_type
*/
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. '', $ 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 .'.
';
@ Extract ($ );
Eval ("\ $ nstr = \" $ str \";");
$ This-> ret. = $ nstr;
$ This-> get_tree_multi ($ id, $ str, $ sid, $ adds. $ k .'');
$ Number ++;
}
}
Return $ this-> ret;
}

Function have ($ list, $ item ){
Return (strpos (','. $ list. ','. $ item .','));
}
}
?>





The http://www.bkjia.com/PHPjc/327686.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327686.htmlTechArticle code is as follows :? Php/*** Common tree class, you can generate any tree structure */class tree {/*** to generate the two-dimensional array required by the tree structure * @ var array */var $ arr =...

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.