广州传奇网络

地址:广州市天河区东圃大马路富华楼C座

电话:13808825895

邮箱:gz020wbs@163.com

QQ:1564443073

网址:http://www.020wbs.com/

首页 > 二次开发Ecshop二次开发 > ecshop无限级分类

二次开发Ecshop二次开发

二次开发Ecshop二次开发

ecshop无限级分类

研究了一下ecshop的无限级商品分类,发现网上并没有人给出对此功能函数的解析,于是为了便于大家更好的理解此函数的算法我写了解析,不足之处与大家交流.
color=Red]此函数采用树的非递归前序遍历进行树的构造
详细见我的blog:ecshop的无限级商品分类详解

  1. [php]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.     if (!isset($cat_options[0])) 
  9.     { 
  10.        /* 
  11.         初始化关键参数: 
  12.         $level:当前子节点深度 
  13.         $last_cat_id:当前父节点ID 
  14.         $options:带有缩进级别的数组 
  15.         $cat_id_array:沿同一路径的父节点依次进驻 
  16.         $level_array:该节点的子节点深度,也是依次进驻 
  17.        */[/php] 

对ecshop无限级分类的解析,认真分析后发现真的其算法还是比较精典的其实并不难理解我有举例方便大家理解

  1. [php] 
  2. function cat_options($spec_cat_id, $arr) 
  3. { 
  4. static $cat_options = array(); 
  5. if (isset($cat_options[$spec_cat_id])) 
  6. { 
  7. return $cat_options[$spec_cat_id]; 
  8. } 
  9. /* 
  10. 初始化关键参数: 
  11. $level:当前子节点深度 
  12. $last_cat_id:当前父节点ID 
  13. $options:带有缩进级别的数组 
  14. $cat_id_array:沿同一路径的父节点依次进驻 
  15. $level_array:该节点的子节点深度,也是依次进驻 
  16. */ 
  17. if (!isset($cat_options[0])) 
  18. { 
  19. $level = $last_cat_id = 0; 
  20. $options = $cat_id_array = $level_array = array(); 
  21. while (!emptyempty($arr))//如果还有待构造的节点则继续遍历 
  22. {  
  23. foreach ($arr AS $key => $value) 
  24. {  
  25. $cat_id = $value['cat_id']; 
  26. //一级分类结点 
  27. if ($level == 0 && $last_cat_id == 0) 
  28. {  
  29. if ($value['parent_id'] > 0) 
  30. { 
  31. break; 
  32. } 
  33. $options[$cat_id] = $value; 
  34. $options[$cat_id]['level'] = $level; 
  35. $options[$cat_id]['id'] = $cat_id; 
  36. $options[$cat_id]['name'] = $value['cat_name']; 
  37. //遍历过了就不再遍历 
  38. unset($arr[$key]); 
  39. if ($value['has_children'] == 0) 
  40. { 
  41. continue; 
  42. } 
  43. $last_cat_id = $cat_id;//下层结点的父亲结点 
  44. $cat_id_array = array($cat_id); 
  45. $level_array[$last_cat_id] = ++$level; 
  46. continue; 
  47. } 
  48. //当前结点的父亲结点ID等于它的上一级结点ID 
  49. if ($value['parent_id'] == $last_cat_id) 
  50. {  
  51. $options[$cat_id] = $value; 
  52. $options[$cat_id]['level'] = $level; 
  53. $options[$cat_id]['id'] = $cat_id; 
  54. $options[$cat_id]['name'] = $value['cat_name']; 
  55. unset($arr[$key]);//遍历过了就不再遍历 
  56. //如果当前结点有孩子则当前结点要进驻,但不再遍历;反之不进驻也不再遍历 
  57. if ($value['has_children'] > 0) 
  58. { 
  59. if (end($cat_id_array) != $last_cat_id) 
  60. { 
  61. $cat_id_array[] = $last_cat_id; 
  62. } 
  63. $last_cat_id = $cat_id;//当现结点做为下一级结点的新的父亲结点 
  64. $cat_id_array[] = $cat_id;//进驻 
  65.  
  66. $level_array[$last_cat_id] = ++$level;//当前结点的下一级结点深度 
  67. } 
  68.  
  69. } 
  70. elseif ($value['parent_id'] > $last_cat_id) 
  71. {//如果当前结点父亲深度大于目前父亲结点的深度则进行下一轮循环 
  72. break; 
  73. } 
  74. }//endforeach 
  75. $count = count($cat_id_array); 
  76. if ($count > 1) 
  77. { 
  78. //取出最后进驻的父亲节点作为当前父亲节点 
  79. $last_cat_id = array_pop($cat_id_array); 
  80. } 
  81. elseif ($count == 1) 
  82. { 
  83. if ($last_cat_id != end($cat_id_array)) 
  84. { 
  85. //进驻的父亲结点只有一个时并且没有作为当前父亲节点时把它取出 
  86. $last_cat_id = end($cat_id_array); 
  87. } 
  88. else 
  89. { //否则最后取出的父亲结点一定是一级分类结点 
  90. $level = 0; 
  91. $last_cat_id = 0; 
  92. $cat_id_array = array(); 
  93. continue; 
  94. } 
  95. } 
  96.  
  97. if ($last_cat_id && isset($level_array[$last_cat_id])) 
  98. {  
  99. //取出当前结点的深度 
  100. $level = $level_array[$last_cat_id]; 
  101. } 
  102. else 
  103. {  
  104. $level = 0; 
  105. } 
  106. }//end while,此时已完成非递归前序遍历构造树的工作,其中$options已保存了从根结点开始的所有结点带有分层性质的数组 
  107. $cat_options[0] = $options; 
  108. } 
  109. else 
  110. { 
  111. $options = $cat_options[0]; 
  112. } 
  113. //如果从0开始即取整个树则直接返回不再处理. 
  114. if (!$spec_cat_id) 
  115. { 
  116. return $options; 
  117. } 
  118. //否则开始从指定结点截取,以下比较简单我还是稍微说说吧,要说就说几个参数含义吧 
  119. /* 
  120. $spec_cat_id_level:截取结点的深度 
  121. $spec_cat_id_array:最终返回的以该结点为根结点的一棵商品分类树 
  122. 最终返回的数组是这样排序的:按父亲结点大小,按直接父亲结点,按同一父亲结点这样的先根遍历,具个例子: 
  123. 一级结点有1,5 二级结点有2,6,7 三级结点有8,9,如果1的直接孩子是2,6而2的直接孩子是8,9;另外 
  124. 5的直接孩子是7那么最终的数组是这样排列的1->2->8->9->6->5->7  
  125. */ 
  126. else 
  127. { 
  128. if (emptyempty($options[$spec_cat_id])) 
  129. { 
  130. return array(); 
  131. } 
  132. $spec_cat_id_level = $options[$spec_cat_id]['level']; 
  133.  
  134. foreach ($options AS $key => $value) 
  135. { 
  136. if ($key != $spec_cat_id) 
  137. { 
  138. unset($options[$key]); 
  139. } 
  140. else 
  141. { 
  142. break; 
  143. } 
  144. } 
  145. $spec_cat_id_array = array(); 
  146. foreach ($options AS $key => $value) 
  147. { 
  148. if (($spec_cat_id_level == $value['level'] && $value['cat_id'] != $spec_cat_id) || 
  149. ($spec_cat_id_level > $value['level'])) 
  150. { 
  151. break; 
  152. } 
  153. else 
  154. { 
  155. $spec_cat_id_array[$key] = $value; 
  156. } 
  157. } 
  158. $cat_options[$spec_cat_id] = $spec_cat_id_array; 
  159. return $spec_cat_id_array; 
  160. } 
  161. } 
  162. [/php]