- /**
- +------------------------------------------------
- * Universal Tree Type
- +------------------------------------------------
- * @author yangyunzhou@foxmail.com
- +------------------------------------------------
- * @date November 23, 2010 10:09:31
- +------------------------------------------------
- */
- Class Tree
- {
- /**
- +------------------------------------------------
- * 2-dimensional array required to generate the tree structure
- +------------------------------------------------
- * @author yangyunzhou@foxmail.com
- +------------------------------------------------
- * @var Array
- */
- var $arr = array ();
- /**
- +------------------------------------------------
- * Create a tree structure with the necessary decoration symbols, can be converted to pictures
- +------------------------------------------------
- * @author yangyunzhou@foxmail.com
- +------------------------------------------------
- * @var Array
- */
- var $icon = array (' │ ', ' ├ ', ' └ ');
- /**
- * @access Private
- */
- var $ret = ';
- /**
- * Constructors, initializing classes
- * @param array of 2-D arrays, for example:
- * Array (
- * 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' first column one '),
- * 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' "one column II '),
- * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '),
- * 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' two ' column two '),
- * 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two ' column three '),
- * 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three ' column one '),
- * 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' three ' level Column II ')
- * )
- */
- function tree ($arr =array ())
- {
- $this->arr = $arr;
- $this->ret = ";
- Return Is_array ($arr);
- }
- /**
- * Get Parent Series 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;
- }
- /**
- * Get sub-series group
- * @param int
- * @return Array
- */
- function 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 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-type structure
- * -------------------------------------
- * @author yangyunzhou@foxmail.com
- * @param $myid means to get all the children under this ID
- * @param $str generate the basic code of the tree structure, for example: "\ $spacer \ $name "
- * @param $sid The selected ID, such as in the Tree drop-down box 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, 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 ': ';
- @extract ($a);
- Eval ("\ $nstr = \" $str \ ";");
- $this->ret. = $nstr;
- $this->get_tree_multi ($id, $str, $sid, $adds. $k. ");
- $number + +;
- }
- }
- return $this->ret;
- }
- function has ($list, $item) {
- Return (Strpos (',, '. $list. ', ', ', '. $item. ', '));
- }
- /**
- +------------------------------------------------
- * Formatted array
- +------------------------------------------------
- * @author yangyunzhou@foxmail.com
- +------------------------------------------------
- */
- 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;
- }
- }
- ?>
Copy Code
- Usage:
- $tree = new Tree; Please remember to include the tree file before new!
- $tree->tree ($data); Please refer to the comments on the tree method for the data format!
- If you use an array, use the GetArray method
- $tree->getarray ();
- Drop-down menu options use the Get_tree method
- $tree->get_tree ();
Copy Code
|