ecshop2.7.0商品分类显示数量

2016-07-07 16:11 来源:www.chinab4c.com 作者:ecshop专家

ecshop商品分类显示数量,以默认模板为例,有附件可以直接下载。
1.jpg
1.修改includes/lib_goods.php,改写下两个函数。

  1. /**
  2. * 获得指定分类同级的所有分类以及该分类下的子分类
  3. *
  4. * @accesspublic
  5. * @paraminteger$cat_id分类编号
  6. * @returnarray
  7. */
  8. function get_categories_tree($cat_id = 0)
  9. {
  10. if ($cat_id > 0)
  11. {
  12. $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
  13. $parent_id = $GLOBALS['db']->getOne($sql);
  14. }
  15. else
  16. {
  17. $parent_id = 0;
  18. }
  19. /*
  20. 判断当前分类中全是是否是底级分类,
  21. 如果是取出底级分类上级分类,
  22. 如果不是取当前分类及其下的子分类
  23. */
  24. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
  25. if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
  26. {
  27. /* 获取当前分类及其子分类 */
  28. $sql = 'SELECT cat_id,cat_name ,parent_id,is_show ' .
  29. 'FROM ' . $GLOBALS['ecs']->table('category') .
  30. "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
  31. $res = $GLOBALS['db']->getAll($sql);

  32. $sql = "SELECT cat_id, COUNT(*) AS goods_num " .
  33. " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
  34. " GROUP BY cat_id";

  35. $res2 = $GLOBALS['db']->getAll($sql);
  36. $newres = array();
  37. foreach($res2 AS $row)
  38. {
  39. $newres[$row['cat_id']] = $row['goods_num'];
  40. }

  41. foreach ($res AS $row)
  42. {
  43. if ($row['is_show'])
  44. {
  45. $cat_arr[$row['cat_id']]['id']= $row['cat_id'];
  46. $cat_arr[$row['cat_id']]['num']= !empty($newres[$row['cat_id']]) ? $newres[$row['cat_id']] : 0;
  47. $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
  48. $cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
  49. if (isset($row['cat_id']) != NULL)
  50. {
  51. $cat_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
  52. }
  53. }
  54. }
  55. }
  56. if(isset($cat_arr))
  57. {
  58. return $cat_arr;
  59. }
  60. }
  61. function get_child_tree($tree_id = 0)
  62. {
  63. $three_arr = array();
  64. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$tree_id' AND is_show = 1 ";
  65. if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
  66. {
  67. $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
  68. 'FROM ' . $GLOBALS['ecs']->table('category') .
  69. "WHERE parent_id = '$tree_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
  70. $res = $GLOBALS['db']->getAll($child_sql);


  71. $sql = "SELECT cat_id, COUNT(*) AS goods_num " .
  72. " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
  73. " GROUP BY cat_id";
  74. $res2 = $GLOBALS['db']->getAll($sql);
  75. $newres = array();
  76. foreach($res2 AS $row)
  77. {
  78. $newres[$row['cat_id']] = $row['goods_num'];
  79. }

  80. foreach ($res AS $row)
  81. {
  82. if ($row['is_show'])
  83. $three_arr[$row['cat_id']]['num']= !empty($newres[$row['cat_id']]) ? $newres[$row['cat_id']] : 0;
  84. $three_arr[$row['cat_id']]['id']= $row['cat_id'];
  85. $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
  86. $three_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
  87. if (isset($row['cat_id']) != NULL)
  88. {
  89. $three_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
  90. }
  91. }
  92. }
  93. return $three_arr;
  94. }
复制代码
2.修改category_tree.lib库,写入num变量。
  1. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  2. <div class="box">
  3. <div class="box_1">
  4. <div id="category_tree">
  5. <!--{foreach from=$categories item=cat}-->
  6. <dl>
  7. <dt><a href="{$cat.url}">{$cat.name|escape:html}</a>({$cat.num})</dt>
  8. <!--{foreach from=$cat.cat_id item=child}-->
  9. <dd><a href="{$child.url}">{$child.name|escape:html}</a>({$child.num})</dd>
  10. <!--{foreach from=$child.cat_id item=childer}-->
  11. <dd><a href="{$childer.url}">{$childer.name|escape:html}</a>({$childer.num})</dd>
  12. <!--{/foreach}-->
  13. <!--{/foreach}-->

  14. </dl>
  15. <!--{/foreach}-->
  16. </div>
  17. </div>
  18. </div>
  19. <div class="blank5"></div>
复制代码

商品分类显示数量.rar (12.77 KB)


回答:
谢谢了,这功能不错