Analysis of infinite classification in ecshop

WBOY
Release: 2016-07-25 09:03:26
Original
731 people have browsed it
  1. function cat_options($spec_cat_id, $arr)

  2. {
  3. static $cat_options = array();
  4. if (isset($cat_options[$spec_cat_id]))
  5. {
  6. return $cat_options[$spec_cat_id];
  7. }
  8. /*
  9. Initialization key parameters:
  10. $level: current child node depth
  11. $last_cat_id: current parent node ID
  12. $options: array with indentation level
  13. $cat_id_array: Parent nodes along the same path are stationed in sequence
  14. $level_array: the depth of the child nodes of this node is also stationed in order
  15. */
  16. if (!isset($cat_options[0]))
  17. {
  18. $level = $last_cat_id = 0;
  19. $options = $cat_id_array = $level_array = array();
  20. while (!empty($arr))//If there are still nodes to be constructed, continue traversing
  21. {
  22. foreach ($arr AS $key => $value)
  23. {
  24. $cat_id = $value['cat_id'];
  25. //Level 1 classification node
  26. if ($level == 0 && $last_cat_id == 0)
  27. {
  28. if ($value['parent_id'] > ; 0)
  29. {
  30. break;
  31. }
  32. $options[$cat_id] = $value;
  33. $options[$cat_id]['level'] = $level;
  34. $options[$cat_id]['id'] = $cat_id;
  35. $options[$cat_id]['name'] = $value['cat_name'];
  36. //It will no longer be traversed after it has been traversed
  37. unset($arr[$key]);
  38. if ($value ['has_children'] == 0)
  39. {
  40. continue;
  41. }
  42. $last_cat_id = $cat_id;//The parent node of the lower node
  43. $cat_id_array = array($cat_id);
  44. $level_array[$last_cat_id] = ++$level;
  45. continue;
  46. }
  47. //The parent node ID of the current node is equal to its upper-level node ID
  48. if ($value['parent_id'] == $last_cat_id)
  49. {
  50. $options [$cat_id] = $value;
  51. $options[$cat_id]['level'] = $level;
  52. $options[$cat_id]['id'] = $cat_id;
  53. $options[$cat_id]['name '] = $value['cat_name'];
  54. unset($arr[$key]);//After traversing, it will no longer be traversed
  55. //If the current node has children, the current node will be stationed, but no longer Traverse; otherwise, it will not be traversed without entering
  56. if ($value['has_children'] > 0)
  57. {
  58. if (end($cat_id_array) != $last_cat_id)
  59. {
  60. $cat_id_array[] = $last_cat_id;
  61. }
  62. $last_cat_id = $cat_id;//When the current node is used as the new parent node of the next-level node
  63. $cat_id_array[] = $cat_id;//Enter

  64. $ level_array[$last_cat_id] = ++$level;//The depth of the next level node of the current node

  65. }

  66. }

  67. elseif ($value['parent_id'] > $last_cat_id )
  68. {//If the depth of the current node’s parent is greater than the depth of the current parent node, proceed to the next cycle
  69. break;
  70. }
  71. }//endforeach
  72. $count = count($cat_id_array);
  73. if ($count > ; 1)
  74. {
  75. //Take out the last entered parent node as the current parent node
  76. $last_cat_id = array_pop($cat_id_array);
  77. }
  78. elseif ($count == 1)
  79. {
  80. if ($last_cat_id != end( $cat_id_array))
  81. {
  82. //When there is only one stationed parent node and it is not used as the current parent node, take it out
  83. $last_cat_id = end($cat_id_array);
  84. }
  85. else
  86. { //Otherwise, the last parent taken out The node must be a first-level classification node
  87. $level = 0;
  88. $last_cat_id = 0;
  89. $cat_id_array = array();
  90. continue;
  91. }
  92. }

  93. if ($last_cat_id && isset($level_array[$last_cat_id]))

  94. {
  95. //Get the depth of the current node
  96. $level = $level_array[$last_cat_id];
  97. }
  98. else
  99. {
  100. $level = 0;
  101. }
  102. }/ /end while, at this time, the work of non-recursive pre-order traversal to construct the tree has been completed, in which $options has saved a hierarchical array of all nodes starting from the root node
  103. $cat_options[0] = $options;
  104. }
  105. else
  106. {
  107. $options = $cat_options[0];
  108. }
  109. //If the entire tree is taken starting from 0, it will be returned directly and no longer processed.
  110. if (!$spec_cat_id)
  111. {
  112. return $options;
  113. }
  114. //Otherwise, start intercepting from the specified node. The following is relatively simple. I will talk about it a little bit. Let me talk about the meaning of a few parameters.
  115. /*
  116. $spec_cat_id_level: The depth of the intercepted node
  117. $spec_cat_id_array: The final return A product classification tree with this node as the root node
  118. The final returned array is sorted like this: by the size of the parent node, by the direct parent node, by the same parent node, and so on. Example:
  119. The first-level node has 1,5, the second-level node has 2,6,7, and the third-level node has 8,9. If the direct child of 1 is 2,6 and the direct child of 2 is 8,9; in addition The direct child of
  120. 5 is 7, then the final array is arranged like this 1->2->8->9->6->5->7
  121. */
  122. else
  123. {
  124. if ( empty($options[$spec_cat_id]))
  125. {
  126. return array();
  127. }
  128. $spec_cat_id_level = $options[$spec_cat_id]['level'];

  129. foreach ($options AS $key => $value)

  130. {
  131. if ($key != $spec_cat_id)
  132. {
  133. unset($options[$key]);
  134. }
  135. else
  136. {
  137. break;
  138. }
  139. }
  140. $spec_cat_id_array = array();
  141. foreach ($options AS $key => $value)
  142. {
  143. if (($spec_cat_id_level == $value['level'] && $value['cat_id'] != $spec_cat_id) | |
  144. ($spec_cat_id_level > $value['level']))
  145. {
  146. break;
  147. }
  148. else
  149. {
  150. $spec_cat_id_array[$key] = $value;
  151. }
  152. }
  153. $cat_options[$spec_cat_id] = $spec_cat_id_array;
  154. return $spec_cat_id_array;
  155. }
  156. }
  157. ?>

Copy code


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template