Php unlimited classification, super simple unlimited classification, support output tree chart _ php skills

Source: Internet
Author: User
Php unlimited classification, super simple unlimited classification, support output tree chart, code from the network, but I have used it for a long time to share it with you without platform restrictions
You only need to inform the id, parentid, and name.

<? Phperror_reporting (E_ALL ^ E_NOTICE ); class Tree {/** + dimensions * The two-dimensional Array + dimensions * @ author abc + ---------------------------------------------------------- * @ var array */var $ arr = Array () required for generating the Tree structure (); /** + ------------------------------------------------ * the modifier required to generate the tree structure. you can change it to an image + -------------------------------------- * @ au Thor abc + -------------------------------------------- * @ 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' => '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 );} /*** get 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 => $ a) {if ($ a ['parentid'] = $ pid) $ newarr [$ id] = $ a;} return $ newarr ;} /*** get the sublevel 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] = $ a ;}} return $ newarr? $ Newarr: false;}/*** get the current position array * @ 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 $ ;} /***** obtain the tree structure ** --------------------------------------------- * @ author abc * @ param $ myid indicates that all sub-levels under this ID are obtained * @ param $ str generates the basic code of the tree structure, example :"\ $ Spacer \ $ name"* @ Param $ ID the selected ID. for example, you need to use * @ param $ adds * @ param $ str_group */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 => $ a) {$ 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 ($ a); $ 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, however, you can select */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 => $ a) {$ j = $ k = ''; if ($ nu Mber = $ 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': ''; @ extract ($ a); 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. ',');}/** + -------------------------------------------------- * format the array + response * @ author abc + --------------------------- --------------------- */Function getArray ($ myid = 0, $ sid = 0, $ adds = '') {$ number = 1; $ child = $ this-> get_child ($ myid); if (is_array ($ child) {$ total = count ($ child ); foreach ($ child as $ id => $ a) {$ j = $ k = ''; if ($ number = $ total) {$ j. = $ this-> icon [2];} else {$ j. = $ this-> icon [1]; $ k = $ adds? $ This-> icon [0]: '';} $ spacer = $ adds? $ Adds. $ j: ''; @ extract ($ a); $ a ['name'] = $ spacer. ''. $ a ['name']; $ this-> ret [$ a ['id'] = $ a; $ fd = $ adds. $ k. ''; $ this-> getArray ($ id, $ sid, $ fd); $ number ++;} return $ this-> ret ;}} $ data = 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 topic 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' => ''), 8 => array ('id' => '8', 'parentid' => 3, 'name' => 'third-level column 3 '), 9 => array ('id' => '9', 'parentid' => 7, 'name' => 'level 4 category 3 '),); $ tree = new Tree; $ tree-> tree ($ data); // If arrays are used, use the getArray method // $ tree-> getArray (); // use the get_tree method for the drop-down menu options $ html =''; $ Str ="\ $ Spacer \ $ name"; $ Html. = $ tree-> get_tree (0, $ str,-1 ).''; Echo $ html;?>

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.