PHP.40-TP框架商城应用实例-后台15-商品属性与库存量1-不同商品(唯一属性、可选属性),属性类型

思路:

1、不同商品属于不同的类型,如:手机、服装、电脑等类型

2、不同的类型有不同的属性,其中分为唯一属性和可选属性,如服装:可选属性{尺寸:S,M,L……;颜色:白色,黑色……}唯一属性:材质

首先把类型与属性关联起来

1、建表

  类型表{p39_type}

drop table if exists p39_type;
create table p39_type
(
    id mediumint unsigned not null auto_increment comment 'Id',
    type_name varchar(30) not null comment '类型名称',
    primary key (id)
)engine=InnoDB default charset=utf8 comment '类型';

  属性表{p39_attribute}

drop table if exists p39_attribute;
create table p39_attribute
(
    id mediumint unsigned not null auto_increment comment 'Id',
    attr_name varchar(30) not null comment '属性名称',
    attr_type enum('唯一', '可选') not null comment '属性类型',
    attr_option_values varchar(300) not null default '' comment '属性可选值,用逗号隔开多个值',
    type_id mediumint unsigned unsigned not null comment '所属类型',
    primary key (id),
    key type_id (type_id)
)engine=InnoDB default charset=utf8 comment '属性表';

2、GII自动生成两张表的操作代码

注意:

1、生成类型表代码前,在配置文件p39_type.php中,注意添加检查类型名称是否重复的验证;且不需要搜索功能【类型数据级不大】

2、在生成属性表代码之前,在配置文件p39_arrtribute中, 删除不必要的搜索字段

3、调整生成代码,使之更适用

1、类型改为下拉框,适用之前封装好的下拉框函数buildSelect('表名','下拉框名','下拉框的值[表字段]','表字段文本','可选值')

  function buildSelect($tableName, $selectName, $valueFieldName, $textFieldName, $selectedValue = '')
    {
        $model = D($tableName);
        $data = $model->field("$valueFieldName,$textFieldName")->select();            //取需要的两个字段值
        $select = "<select name='$selectName'><option value=''>请选择</option>";        //下拉框name即存进表字段
        foreach ($data as $k => $v)
        {
            $value = $v[$valueFieldName];        //值value <= 字段值【表id】
            $text = $v[$textFieldName];            //显示内容 <= 字段值存放内容
            if($selectedValue && $selectedValue==$value)    //判断是否选中
                $selected = 'selected="selected"';
            else
                $selected = '';
            $select .= '<option '.$selected.' value="'.$value.'">'.$text.'</option>';
        }
        $select .= '</select>';
        echo $select;
    }

2、对属性可选值出现中文逗号情况的处理

 删除类型

删除类型时,类型对应下的属性全部删除

思路:在删除类型代码TypeModel.class.php执行前,删除所有属性{_before_delete()}

 

 

 

 

posted on 2017-06-21 21:04  子轩非鱼  阅读(744)  评论(0编辑  收藏  举报

导航