所在位置:首页 > 学习心得 > ecshop商品分类显示数量
ecshop商品分类显示数量
  • ecshop商品分类显示数量
    发布时间:2010-12-13 类别:PHP技术 来源:侠客工作室
    • 1
    • 2
    • 3
    • 4
    • 5
  • 1.修改includes/lib_goods.php,改写下两个函数。


    /**
    * 获得指定分类同级的所有分类以及该分类下的子分类
    *
    * @access  public
    * @param   integer     $cat_id     分类编号
    * @return  array
    */
    function get_categories_tree($cat_id = 0)
    {
        if ($cat_id > 0)
       
            $sql = ‘SELECT parent_id FROM ‘ . $GLOBALS[‘ecs‘]->table(‘category‘) . " WHERE cat_id = ‘$cat_id‘";
            $parent_id = $GLOBALS[‘db‘]->getOne($sql);
      
        else
       
            $parent_id = 0;
      
        /*
         判断当前分类中全是是否是底级分类,
         如果是取出底级分类上级分类,
         如果不是取当前分类及其下的子分类
        */
        $sql = ‘SELECT count(*) FROM ‘ . $GLOBALS[‘ecs‘]->table(‘category‘) . " WHERE parent_id = ‘$parent_id‘ AND is_show = 1 ";
        if ($GLOBALS[‘db‘]->getOne($sql) || $parent_id == 0)
        {
            /* 获取当前分类及其子分类 */
            $sql = ‘SELECT cat_id,cat_name ,parent_id,is_show ‘ .
                    ‘FROM ‘ . $GLOBALS[‘ecs‘]->table(‘category‘) .
                    "WHERE parent_id = ‘$parent_id‘ AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
            $res = $GLOBALS[‘db‘]->getAll($sql);

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

        $res2 = $GLOBALS[‘db‘]->getAll($sql);
        $newres = array();
        foreach($res2 AS $row)
       
         $newres[$row[‘cat_id‘]] = $row[‘goods_num‘];
      

            foreach ($res AS $row)
            {
                if ($row[‘is_show‘])
                {
                    $cat_arr[$row[‘cat_id‘]][‘id‘]   = $row[‘cat_id‘];
            $cat_arr[$row[‘cat_id‘]][‘num‘]  = !empty($newres[$row[‘cat_id‘]]) ? $newres[$row[‘cat_id‘]] : 0;
                    $cat_arr[$row[‘cat_id‘]][‘name‘] = $row[‘cat_name‘];
                    $cat_arr[$row[‘cat_id‘]][‘url‘]  = build_uri(‘category‘, array(‘cid‘ => $row[‘cat_id‘]), $row[‘cat_name‘]);
                    if (isset($row[‘cat_id‘]) != NULL)
                   
                        $cat_arr[$row[‘cat_id‘]][‘cat_id‘] = get_child_tree($row[‘cat_id‘]);
                  
                }
            }
        }
        if(isset($cat_arr))
       
            return $cat_arr;
      
    }
    function get_child_tree($tree_id = 0)
    {
        $three_arr = array();
        $sql = ‘SELECT count(*) FROM ‘ . $GLOBALS[‘ecs‘]->table(‘category‘) . " WHERE parent_id = ‘$tree_id‘ AND is_show = 1 ";
        if ($GLOBALS[‘db‘]->getOne($sql) || $tree_id == 0)
        {
            $child_sql = ‘SELECT cat_id, cat_name, parent_id, is_show ‘ .
                    ‘FROM ‘ . $GLOBALS[‘ecs‘]->table(‘category‘) .
                    "WHERE parent_id = ‘$tree_id‘ AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
            $res = $GLOBALS[‘db‘]->getAll($child_sql);


       $sql = "SELECT cat_id, COUNT(*) AS goods_num " .
       " FROM " . $GLOBALS[‘ecs‘]->table(‘goods‘) . " AS g " .
       " GROUP BY cat_id";
        $res2 = $GLOBALS[‘db‘]->getAll($sql);
        $newres = array();
        foreach($res2 AS $row)
       
         $newres[$row[‘cat_id‘]] = $row[‘goods_num‘];
      

            foreach ($res AS $row)
            {
                if ($row[‘is_show‘])
            $three_arr[$row[‘cat_id‘]][‘num‘]  = !empty($newres[$row[‘cat_id‘]]) ? $newres[$row[‘cat_id‘]] : 0;
                   $three_arr[$row[‘cat_id‘]][‘id‘]   = $row[‘cat_id‘];
                   $three_arr[$row[‘cat_id‘]][‘name‘] = $row[‘cat_name‘];
                   $three_arr[$row[‘cat_id‘]][‘url‘]  = build_uri(‘category‘, array(‘cid‘ => $row[‘cat_id‘]), $row[‘cat_name‘]);
                   if (isset($row[‘cat_id‘]) != NULL)
                      
                           $three_arr[$row[‘cat_id‘]][‘cat_id‘] = get_child_tree($row[‘cat_id‘]);
              
            }
        }
        return $three_arr;
    }

    复制代码
    2.修改category_tree.lib库,写入num变量。
    <meta http-equiv="Content-Type" content="text/html; charset=gbk">
    <div class="box">
    <div class="box_1">
    <div id="category_tree">
    <!--foreach from=$categories item=cat-->
    <dl>
    <dt><a href="$cat.url">escape:html</a>($cat.num)</dt>
    <!--foreach from=$cat.cat_id item=child-->
    <dd><a href="$child.url">escape:html</a>($child.num)</dd>
    <!--foreach from=$child.cat_id item=childer-->
    <dd>  <a href="$childer.url">escape:html</a>($childer.num)</dd>
    <!--/foreach-->
    <!--/foreach-->

    </dl>
    <!--/foreach-->
    </div>
    </div>

  • ☆☆☆技术交流群:5684178☆☆☆
  • 上一篇:ecshop模板如何显示销售排行
  • 下一篇:php文章批量删除
侠客工作室主要承揽网站制作|企业建站|北京网站制作|北京网站建设|北京企业建站|专业仿站|SEO优化等业务
地址:北京市海定区清河水木天成 电话:13718388147 13718388147 传真:13718388147 访问统计:
投诉电话:13718388147 投诉QQ:点击这里给我发消息 客服信箱:1300105556@qq.com 邮编:102200 北京网站建设地图 北京网站建设RSS地图
版权所有 2009-2014侠客工作室【北京网站建设】 京ICP备11021494号-1