PHP.29-TP框架商城应用实例-后台6-商品会员添加-价格、级别

首先把需求分析搞清楚

主要实现两个功能

1、会员管理,设置成为会员的要求

2、添加商品时,可设置会员优惠价格

 

具体实现

1、建表【会员级别限定表p39_member_level{Id,级别名称,积分下限,积分上限}、会员价格表p39_member_price{会员价格,积分Id,商品Id}】

drop table if exists p39_member_level;
create table p39_member_level
(
    id mediumint unsigned not null auto_increment comment 'Id',
    level_name varchar(30) not null comment '级别名称',
    jifen_bottom mediumint unsigned not null comment '积分下限',
    jifen_top mediumint unsigned not null comment '积分上限',
    primary key (id)
)engine=InnoDB default charset=utf8 comment '会员级别';
p39_member_level
drop table if exists p39_member_price;
create table p39_member_price
(
    price decimal(10,2) not null comment '会员价格',
    level_id mediumint unsigned not null comment '级别Id',
    goods_id mediumint unsigned not null comment '商品Id',
    key level_id(level_id),
    key goods_id(goods_id)
)engine=InnoDB default charset=utf8 comment '会员价格';
p39_member_price

2、使用Gii生成代码

配置代码生成配置文件/Gii/Table_configs/p39_member_level.php【会员级别的数量级太少,不需搜索表单】

测试

 3、在添加商品页面可以设置会员价格

3.1修改控制器GoodsController.class.php-add(),使添加页面打印所有的会员级别

3.2修改add.html表单,循环输出【因为是多个会员级别,而name需要插入库,应为会员级别id,所以使用数组存储多个会员价格】

 3.3提交表单,会员价格数据插入会员价格表{会员价格,积分Id,商品Id}

 注:因为为商品插入会员价格,需要获取商品id,而商品id只有在添加商品成功后才生成,所以使用钩子函数_after_insert()

1、价格必须为数字,强制转换为float;  2、价格必须大于0,无数值无法插入,if($_v>0)

//钩子方法_after_insert:添加操作成功后执行
        protected function _after_insert($data, $option)
        {$mp = I('post.member_price');    //接收post提交过来的会员价格数据
            $mpModel = D('member_price');
            foreach ($mp as $k => $v)
            {
                $_v = (float)$v;    //强制转为浮点型,以免插入字符等错误数据
                //设置会员价格>0就插入到表中
                if($_v > 0)
                {
                    $mpModel->add(array(
                        'price' => $_v,
                        'level_id' => $k,        //级别Id
                        'goods_id' => $data['id'],
                    ));
                }
            }
        }

$data => 将要插入商品表中数据

测试

 

 

优化商品添加表单js

 管理首页,左侧按钮连接到页面

 

 

 

 

posted on 2017-06-08 00:25  子轩非鱼  阅读(287)  评论(0编辑  收藏  举报

导航