PHP Unlimited Classification [Enhanced Edition]

Source: Internet
Author: User
  1. /**
  2. +------------------------------------------------
  3. * Universal Tree Type
  4. +------------------------------------------------
  5. * @author yangyunzhou@foxmail.com
  6. +------------------------------------------------
  7. * @date November 23, 2010 10:09:31
  8. +------------------------------------------------
  9. */
  10. Class Tree
  11. {
  12. /**
  13. +------------------------------------------------
  14. * 2-dimensional array required to generate the tree structure
  15. +------------------------------------------------
  16. * @author yangyunzhou@foxmail.com
  17. +------------------------------------------------
  18. * @var Array
  19. */
  20. var $arr = array ();
  21. /**
  22. +------------------------------------------------
  23. * Create a tree structure with the necessary decoration symbols, can be converted to pictures
  24. +------------------------------------------------
  25. * @author yangyunzhou@foxmail.com
  26. +------------------------------------------------
  27. * @var Array
  28. */
  29. var $icon = array (' │ ', ' ├ ', ' └ ');
  30. /**
  31. * @access Private
  32. */
  33. var $ret = ';
  34. /**
  35. * Constructors, initializing classes
  36. * @param array of 2-D arrays, for example:
  37. * Array (
  38. * 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' first column one '),
  39. * 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' "one column II '),
  40. * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '),
  41. * 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' two ' column two '),
  42. * 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two ' column three '),
  43. * 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three ' column one '),
  44. * 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' three ' level Column II ')
  45. * )
  46. */
  47. function tree ($arr =array ())
  48. {
  49. $this->arr = $arr;
  50. $this->ret = ";
  51. Return Is_array ($arr);
  52. }
  53. /**
  54. * Get Parent Series Group
  55. * @param int
  56. * @return Array
  57. */
  58. function Get_parent ($myid)
  59. {
  60. $newarr = Array ();
  61. if (!isset ($this->arr[$myid])) return false;
  62. $pid = $this->arr[$myid] [' ParentID '];
  63. $pid = $this->arr[$pid] [' ParentID '];
  64. if (Is_array ($this->arr))
  65. {
  66. foreach ($this->arr as $id = $a)
  67. {
  68. if ($a [' parentid '] = = $pid) $newarr [$id] = $a;
  69. }
  70. }
  71. return $newarr;
  72. }
  73. /**
  74. * Get sub-series group
  75. * @param int
  76. * @return Array
  77. */
  78. function Get_child ($myid)
  79. {
  80. $a = $newarr = Array ();
  81. if (Is_array ($this->arr))
  82. {
  83. foreach ($this->arr as $id = $a)
  84. {
  85. if ($a [' parentid '] = = $myid) $newarr [$id] = $a;
  86. }
  87. }
  88. Return $newarr? $newarr: false;
  89. }
  90. /**
  91. * Get the current position array
  92. * @param int
  93. * @return Array
  94. */
  95. function Get_pos ($myid,& $newarr)
  96. {
  97. $a = array ();
  98. if (!isset ($this->arr[$myid])) return false;
  99. $newarr [] = $this->arr[$myid];
  100. $pid = $this->arr[$myid] [' ParentID '];
  101. if (Isset ($this->arr[$pid]))
  102. {
  103. $this->get_pos ($pid, $newarr);
  104. }
  105. if (Is_array ($newarr))
  106. {
  107. Krsort ($newarr);
  108. foreach ($newarr as $v)
  109. {
  110. $a [$v [' id ']] = $v;
  111. }
  112. }
  113. return $a;
  114. }
  115. /**
  116. * -------------------------------------
  117. * Get tree-type structure
  118. * -------------------------------------
  119. * @author yangyunzhou@foxmail.com
  120. * @param $myid means to get all the children under this ID
  121. * @param $str generate the basic code of the tree structure, for example: "\ $spacer \ $name "
  122. * @param $sid The selected ID, such as in the Tree drop-down box need to use
  123. * @param $adds
  124. * @param $str _group
  125. */
  126. function Get_tree ($myid, $str, $sid = 0, $adds = ", $str _group =")
  127. {
  128. $number = 1;
  129. $child = $this->get_child ($myid);
  130. if (Is_array ($child)) {
  131. $total = count ($child);
  132. foreach ($child as $id = = $a) {
  133. $j = $k = ";
  134. if ($number = = $total) {
  135. $j. = $this->icon[2];
  136. } else {
  137. $j. = $this->icon[1];
  138. $k = $adds? $this->icon[0]: ";
  139. }
  140. $spacer = $adds? $adds. $j: ";
  141. $selected = $id = = $sid? ' Selected ': ';
  142. @extract ($a);
  143. $parentid = = 0 && $str _group? Eval ("\ $nstr = \" $str _group\ "): eval (" \ $nstr = \ "$str \"; ");
  144. $this->ret. = $nstr;
  145. $this->get_tree ($id, $str, $sid, $adds. $k. ", $str _group);
  146. $number + +;
  147. }
  148. }
  149. return $this->ret;
  150. }
  151. /**
  152. * Similar to the previous method, but allows multiple selections
  153. */
  154. function Get_tree_multi ($myid, $str, $sid = 0, $adds = ")
  155. {
  156. $number = 1;
  157. $child = $this->get_child ($myid);
  158. if (Is_array ($child))
  159. {
  160. $total = count ($child);
  161. foreach ($child as $id = $a)
  162. {
  163. $j = $k = ";
  164. if ($number = = $total)
  165. {
  166. $j. = $this->icon[2];
  167. }
  168. Else
  169. {
  170. $j. = $this->icon[1];
  171. $k = $adds? $this->icon[0]: ";
  172. }
  173. $spacer = $adds? $adds. $j: ";
  174. $selected = $this->have ($sid, $id)? ' Selected ': ';
  175. @extract ($a);
  176. Eval ("\ $nstr = \" $str \ ";");
  177. $this->ret. = $nstr;
  178. $this->get_tree_multi ($id, $str, $sid, $adds. $k. ");
  179. $number + +;
  180. }
  181. }
  182. return $this->ret;
  183. }
  184. function has ($list, $item) {
  185. Return (Strpos (',, '. $list. ', ', ', '. $item. ', '));
  186. }
  187. /**
  188. +------------------------------------------------
  189. * Formatted array
  190. +------------------------------------------------
  191. * @author yangyunzhou@foxmail.com
  192. +------------------------------------------------
  193. */
  194. function GetArray ($myid =0, $sid =0, $adds = ")
  195. {
  196. $number = 1;
  197. $child = $this->get_child ($myid);
  198. if (Is_array ($child)) {
  199. $total = count ($child);
  200. foreach ($child as $id = = $a) {
  201. $j = $k = ";
  202. if ($number = = $total) {
  203. $j. = $this->icon[2];
  204. } else {
  205. $j. = $this->icon[1];
  206. $k = $adds? $this->icon[0]: ";
  207. }
  208. $spacer = $adds? $adds. $j: ";
  209. @extract ($a);
  210. $a [' name '] = $spacer. ' '. $a [' name '];
  211. $this->ret[$a [' id ']] = $a;
  212. $FD = $adds. $k. ";
  213. $this->getarray ($id, $sid, $FD);
  214. $number + +;
  215. }
  216. }
  217. return $this->ret;
  218. }
  219. }
  220. ?>
Copy Code
    1. Usage:
    2. $tree = new Tree; Please remember to include the tree file before new!
    3. $tree->tree ($data); Please refer to the comments on the tree method for the data format!
    4. If you use an array, use the GetArray method
    5. $tree->getarray ();
    6. Drop-down menu options use the Get_tree method
    7. $tree->get_tree ();
Copy Code
Enhanced version, PHP
  • Related Article

    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.