Home Backend Development PHP Tutorial PHP unlimited classification [enhanced version]

PHP unlimited classification [enhanced version]

Jul 25, 2016 am 08:42 AM

  1. /**
  2. +------------------------------------------------
  3. * Universal tree class
  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 needed to generate tree structure
  15. +------------------------------------ ------------
  16. * @author yangyunzhou@foxmail.com
  17. +-------------------------- --------------------------
  18. * @var Array
  19. */
  20. var $arr = array();
  21. /**
  22. +------------------------------------------------
  23. * The modification symbols required to generate a tree structure can be replaced by 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. * Constructor, initialize class
  36. * @param array 2-dimensional array, for example:
  37. * array(
  38. * 1 => array('id'=>'1','parentid'=>0,'name '=>'First-level column one'),
  39. * 2 => array('id'=>'2','parentid'=>0,'name'=>'First-level column two' ),
  40. * 3 => array('id'=>'3','parentid'=>1,'name'=>'Second-level column one'),
  41. * 4 => array( 'id'=>'4','parentid'=>1,'name'=>'Second-level column two'),
  42. * 5 => array('id'=>'5', 'parentid'=>2,'name'=>'Second-level column three'),
  43. * 6 => array('id'=>'6','parentid'=>3,'name '=>'Third-level column one'),
  44. * 7 => array('id'=>'7','parentid'=>3,'name'=>'Third-level column two' )
  45. * )
  46. */
  47. function tree($arr=array())
  48. {
  49. $this->arr = $arr;
  50. $this->ret = '';
  51. return is_array($arr);
  52. }
  53. /**
  54. * Get the parent array
  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 the child array
  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 structure
  118. * --- ----------------------------------
  119. * @author yangyunzhou@foxmail.com
  120. * @param $myid said Get all the children under this ID
  121. * @param $str Generate the basic code of the tree structure, for example: ""
  122. * @param $sid is The selected ID, for example, needed when making a tree drop-down box
  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 have($list,$item){
  185. return(strpos(',,'.$list.',',','.$item.','));
  186. }
  187. /**
  188. +------------------------------------------------
  189. * Format 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. ?>
复制代码

  1. 用法:
  2. $tree = new Tree; // new 之前请记得包含tree文件!
  3. $tree->tree($data); // 数据格式请参考 tree方法上面的注释!
  4. // 如果使用数组, 请使用 getArray方法
  5. $tree->getArray();
  6. // 下拉菜单选项使用 get_tree方法
  7. $tree->get_tree();
复制代码

增强版, PHP


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles