请问如何让模板显示每个分类里面物品的数量

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

稍微修改get_categories_tree 函数(lib_goods.php),增加SQL查询(统计目录商品数量),返回数组中加入数量参数。

再在goods.dwt中适当修改。

回答:
顶楼上。。。。。。。

嗯,谢谢,试试看

在需要页面添加sql统计语句就ok了吧。

  1. function get_categories_tree($cat_id = 0)
  2. {
  3. if ($cat_id > 0)
  4. {
  5. $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
  6. $parent_id = $GLOBALS['db']->getOne($sql);
  7. }
  8. else
  9. {
  10. $parent_id = 0;
  11. }

  12. /*
  13. 判断当前分类中全是是否是底级分类,
  14. 如果是取出底级分类上级分类,
  15. 如果不是取当前分类及其下的子分类
  16. */
  17. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id' AND is_show = 1 ";
  18. if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
  19. {
  20. /* 获取当前分类及其子分类 */
  21. $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, a.is_show,' .
  22. 'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' .
  23. 'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
  24. 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
  25. "WHERE a.parent_id = '$parent_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC";
  26. }
  27. else
  28. {
  29. /* 获取当前分类及其父分类 */
  30. $sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order, b.is_show ' .
  31. 'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
  32. 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
  33. "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC";
  34. }
  35. $res = $GLOBALS['db']->getAll($sql);

  36. $cat_arr = array();
  37. foreach ($res AS $row)
  38. {
  39. if ($row['is_show'])
  40. {
  41. $cat_arr[$row['cat_id']]['id']= $row['cat_id'];
  42. $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
  43. $cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  44. if ($row['child_id'] != NULL)
  45. {
  46. $cat_id = $row['child_id'];
  47. $sql = "select count(*) as num from ".$GLOBALS['ecs']->table('goods')." where cat_id='$cat_id' and is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 ";
  48. $num = $GLOBALS['db']->getOne($sql);
  49. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['num']= $num;
  50. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['id']= $row['child_id'];
  51. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name'];
  52. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['url']= build_uri('category', array('cid' => $row['child_id']), $row['child_name']);
  53. }
  54. }
  55. }

  56. return $cat_arr;
  57. }
复制代码
$sql = "select count(*) as num from ".$GLOBALS['ecs']->table('goods')." where cat_id='$cat_id' and is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 ";
$num = $GLOBALS['db']->getOne($sql);
$cat_arr[$row['cat_id']]['children'][$row['child_id']]['num']= $num;

6# qiyongdong
感谢分享;有计划试试这段代码



谢谢啊!!!
不过,还是有点疑问:
我希望网站每个页面上,只要有分类树的地方,每个分类下面就显示物品的数量.

打个比方, catalog.php页面默认 是如下显示的:

动画影视系列(20)
家饰小挂件(33)
熊熊系列(11)
狗狗系列(10
兔子系列(30



我希望在其他页面也都能这么显示出来. 直接在category_tree.lbi 修改是不行的,请问还需要添加什么sql语句吗?