<?php /** * Universal Tree class, which can generate any tree-type structure */ Class Tree { /** * 2-D arrays required to generate a tree structure * @var Array */ var $arr = array (); /** * Create the tree structure of the required modifiers, you can change to a picture * @var Array */ var $icon = array (' │ ', ' ├ ', ' └ '); /** * @access Private */ var $ret = '; /** * Constructors, initializing classes * @param array of array 2 dimensions, for example: * Array ( * 1 => Array (' ID ' => ' 1 ', ' ParentID ' =>0, ' name ' => ' one-level column one '), * 2 => 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 ' => ' Level two column two '), * 5 => Array (' ID ' => ' 5 ', ' ParentID ' =>2, ' name ' => ' Level two column '), * 6 => Array (' ID ' => ' 6 ', ' ParentID ' =>3, ' name ' => ' Level three column one '), * 7 => Array (' ID ' => ' 7 ', ' ParentID ' =>3, ' name ' => ' Level three column two ') * ) */ function tree ($arr =array ()) { $this->arr = $arr; $this->ret = '; Return Is_array ($arr); } /** * Get the parent progression Group * @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; } /** * Gets the subgroup group * @param int * @return array */ func tion Get_child ($myid) { $a = $newarr = Array (); if (Is_array ($this->arr)) { foreach ($this->arr as $id => $a) { if ($a [' parentid '] = = $myid) $newarr [$id] = $a; } } return $newarr? $newarr: false; } /** * Get the current location array * @param int * @return array */ fu Nction 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; & nbsp;} } return $a; } /** * ------------------------------------- * Get a tree-shaped structure * ------------------------------------- * @author Midnight (Yang Yunzhou), yangyunzhou@foxmail.com * @param $myid means to get all the children under this ID * @param $str Generate basic code for the tree structure, for example: "<option value= $id $select > $spacer $name</option> " * @param $sid the selected ID, for example, when making 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 => $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 selections */ 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 ($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. ')); } } ?> |