国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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. ?>
復(fù)制代碼

  1. 用法:
  2. $tree = new Tree; // new 之前請(qǐng)記得包含tree文件!
  3. $tree->tree($data); // 數(shù)據(jù)格式請(qǐng)參考 tree方法上面的注釋!
  4. // 如果使用數(shù)組, 請(qǐng)使用 getArray方法
  5. $tree->getArray();
  6. // 下拉菜單選項(xiàng)使用 get_tree方法
  7. $tree->get_tree();
復(fù)制代碼

增強(qiáng)版, 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

PHP Tutorial
1501
276
PHP Variable Scope Explained PHP Variable Scope Explained Jul 17, 2025 am 04:16 AM

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

How to handle File Uploads securely in PHP? How to handle File Uploads securely in PHP? Jul 08, 2025 am 02:37 AM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

Commenting Out Code in PHP Commenting Out Code in PHP Jul 18, 2025 am 04:57 AM

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Learning PHP: A Beginner's Guide Learning PHP: A Beginner's Guide Jul 18, 2025 am 04:54 AM

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

How to access a character in a string by index in PHP How to access a character in a string by index in PHP Jul 12, 2025 am 03:15 AM

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

Quick PHP Installation Tutorial Quick PHP Installation Tutorial Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

See all articles