zoukankan      html  css  js  c++  java
  • PHP.37-TP框架商城应用实例-后台13-商品管理-扩展分类的添加、显示【数据分组】、搜索分类【多对多】

    商品扩展分类

    需求:一件商品能有多个扩展分类,搜索任何一个分类都能搜出该商品

    建表【扩展分类表】

    drop table if exists p39_goods_cat;
    create table p39_goods_cat
    (
        cat_id mediumint unsigned not null comment '分类Id',
        goods_id mediumint unsigned not null comment '商品Id',
        key goods_id(goods_id),
        key cat_id(cat_id)
    )engine=InnoDB default charset=utf8 comment '扩展分类';
    goods_cat

    添加扩展分类

    1、在add.html页面中增加扩展分类下拉框,利用JS代码实现多个扩展分类的增加

      JS:点击一次克隆一个扩展分类的下拉框

    2、在模型类GoodsModel.class.php/_before_insert()中处理扩展分类数据【以下是扩展分类的数据】

    显示扩展分类【在lst.html】

    1、修改模型类GoodsModel.class.php中search方法【多表查询{goods(商品表)、goods_cat(扩展分类表)、category(分类表)}】

      扩展:group():结合合计函数,根据一个或多个列对结果集进行分组

      group_concat():数据库函数,能把相同分组的不同行合并起来,在同一行显示

    2、在lst.html页面中显示

    搜索分类时无论商品的主分类或扩展分类属于搜索的分类或其子分类,都属于搜索范围

    思路:选择搜索分类时,得到搜索分类的id($catId),根据$catId,把其子分类的id也找出来($children),把$catId放到$children[]中。则所有的分类id已找到=>$children。根据$children,搜索所有主分类属于$children的商品;搜索所有扩展分类属于$children的商品;交并这两个结果数组,得搜索结果。

    1、定义一个函数getGoodsIdByCatId,取出一个搜索分类搜索出来的所有商品ID

        /**
             *    取出一个分类下所有商品的ID【既考虑主分类也考虑扩展分类】
             **/
            public function getGoodsIdByCatId($catId)
            {
                //先取出所有子分类的ID        【$catId=>传入是搜索的分类ID】
                $catModel = D('category');
                $children = $catModel->getChildren($catId);
                //和子分类放一起            【子分类为扩展分类,也应该被搜索出来】
                $children[] = $catId;
                /********************取出主分类或者扩展分类在这下分类中的商品*******/
                //取出主分类下的商品Id
                $gids = $this->field('id')->where(array(
                    'cat_id' => array('in', $children),        //主分类下的商品
                ))->select();
                //取出扩展分类下的商品ID
                $gcModel = M('goods_cat');
                $gids1 = $gcModel->field('DISTINCT goods_id id')->where(array(
                    'cat_id' => array('IN', $children)
                ))->select();
                //把主分类的ID和扩展分类下的商品ID合并成二维数据【两个都不为空】
                if($gids && $gids1)
                    $gids = array_merge($gids, $gids1);        //合并数组
                elseif ($gids1)
                    $gids = $gids1;
                //二维转一维
                $id = array();
                foreach ($gids as $k => $v)
                {
                    if(!in_array($v['id'], $id))
                        $id[] = $v['id'];
                }
                return $id;
            }

       2、根据商品id,搜索商品表

     

     

     

  • 相关阅读:
    .NET Interop 工具集
    关于正弦波的算法
    Windows Phone 系列 本地数据存储
    Xaml cannot create an instance of “X”
    Windows Phone 系列 使用 MVVM绑定时无法获取当前值
    Windows Phone 系列 应用程序图标无法显示
    Windows Phone 系列 WPConnect无法上网的问题
    Windows Phone 系列 使用 Windows Phone 保存铃声任务
    WP7.5提交应用
    Windows Phone 系列 动态删除ObservableCollection
  • 原文地址:https://www.cnblogs.com/zixuanfy/p/7028202.html
Copyright © 2011-2022 走看看