产品分类树后面显示商品数量,论坛里面的方法都有问题

2016-07-07 15:02 来源:www.chinab4c.com 作者:ecshop专家




几个方法都试过了, 有的只显示小类, 有的显示数量错误的, 比如这个, 大类下面没有子类就正确, 有子类就显示0,而不是小类的总和。
谁能告诉我为什么???
lib_goods.php修改方法

  1. $res = $GLOBALS['db']->getAll($sql);
  2. $sql = "SELECT cat_id, COUNT(*) AS goods_num " . // 分类树数量,此行开始
  3. " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
  4. " GROUP BY cat_id";

  5. $res2 = $GLOBALS['db']->getAll($sql);
  6. $newres = array();
  7. foreach($res2 AS $row)
  8. {
  9. $newres[$row['cat_id']] = $row['goods_num'];
  10. }// 分类树数量,此行结束

  11. $cat_arr[$row['cat_id']]['id']= $row['cat_id'];
  12. $cat_arr[$row['cat_id']]['num']= !empty($newres[$row['cat_id']]) ? $newres[$row['cat_id']] : 0; // 分类树数量
  13. $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
  14. $cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  15. ------------
  16. $res = $GLOBALS['db']->getAll($child_sql);

  17. $sql = "SELECT cat_id, COUNT(*) AS goods_num " . // 分类树数量,此行开始
  18. " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
  19. " GROUP BY cat_id";
  20. $res2 = $GLOBALS['db']->getAll($sql);
  21. $newres = array();
  22. foreach($res2 AS $row)
  23. {
  24. $newres[$row['cat_id']] = $row['goods_num'];
  25. } // 分类树数量,此行结束

  26. $three_arr[$row['cat_id']]['num']= !empty($newres[$row['cat_id']]) ? $newres[$row['cat_id']] : 0;// 分类数数量
复制代码


category_tree.lbi

<!--{foreach from=$categories item=cat}-->
<dd><a href="{$cat.url}" title="{$cat.name|escape:html}">{$cat.name|escape:html}({$cat.num})</a>
<!-- {if $cat.cat_id} -->

<!--{foreach from=$cat.cat_id item=child}-->
<dt><a href="{$child.url}" title="{$child.name|escape:html}">{$child.name|escape:html} ({$child.num})</a></dt>
<!--{/foreach}-->

<!--{/if}-->
</dd>
<!--{/foreach}-->

回答:
你没理解分类的概念.