PHP Infinite Classification, support output tree graph

Source: Internet
Author: User
Tags foreach array count eval
 

<?php/** * Universal Tree class, can generate any tree structure/class Tree {/** * The 2-D array required to generate the structure * @var array/var $arr = array ();/** * Spanning tree structure required Decorative symbols, you can replace the picture * @var array */var $icon = array (' │ ', ' ├ ', ' └ '); /** * @access Private */var $ret = '; /** * constructors, initializing class * @param array 2-D, for example: * Array (* 1 => array (' ID ' => ' 1 ', ' ParentID ' =>0, ' name ' => ' one-level column one '), * 2 =& Gt Array (' ID ' => ' 2 ', ' ParentID ' =>0, ' name ' => ' one-level column II '), * 3 => array (' ID ' => ' 3 ', ' ParentID ' =>1 ', ' name ') => ' Level two column one '), * 4 => Array (' ID ' => ' 4 ', ' ParentID ' =>1, ' name ' => ' two column II '), * 5 => Array (' ID ' => ' 5 ', ' parentid ' = >2, ' name ' => ' two column III '), * 6 => Array (' ID ' => ' 6 ', ' ParentID ' =>3, ' name ' => ' three-level column one '), * 7 => Array (' id ' = > ' 7 ', ' ParentID ' =>3, ' name ' => ' three column II ') * */function tree ($arr =array ()) {$this->arr = $arr; $this->ret = ' " ; Return Is_array ($arr); /** * Gets the parent series * @param int * @return Array/function get_parent ($myid) {$newarr = array (); if (!isset ($this->arr[$my ID]), 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;} r Eturn $newarr; /** * Obtains the subgroup Group * @param int * @return Array */function Get_child ($myid) {$a = $newarr = Array (); if (Is_array ($this->a RR)) {foreach ($this->arr as $id => $a) {if ($a [' parentid '] = = $myid) $newarr [$id] = $a;}} return $newarr? $newarr: false; /** * Gets 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 $a; /** *-------------------------------------* GET tree structure *-------------------------------------* @author midnight (Yang Yunzhou), y angyunzhou@foxmail.com * @param$myid means to get all the children under this ID @param $str generate the tree-structured codebase, for example: "<option value=\ $id \ $select >\ $spacer \ $name </option>" * @p Aram $sid The selected ID, such as the need to use the * @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)) {$tota L = 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; /** * The previous method is similar, but allows multiple select/function Get_tree_multi ($myid, $str, $sid = 0, $adds = ') {$number =1; $child = $this->get_ch ILD ($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 = $this->have ($sid, $id)? ' Selected ': '; echo $sid. ' => '. $id. ': '. $selected. <br/> '; @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. ', ')}} ?>



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.