需求分析:
1. 会员级别管理 [级别名称, 积分下限, 积分上限] 比如注册会员
2. 添加商品时, 可以为每个级别单独指定一个级别价格
第一步 建表
- 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 '会员级别';
建会员价格表
- 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 '会员价格';
使用 gii 生成会员级别的代码
配置文件
http://localhost:8989/PHP/TpShop/index.PHP/Gii
输入 : p39_member_level
配置文件 gii/tab_config:
- <?PHP
- return array(
- 'tableName' => 'p39_member_level', // 表名
- 'tableCnName' => '会员级别', // 表的中文名
- 'moduleName' => 'Admin', // 代码生成到的模块
- 'withPrivilege' => FALSE, // 是否生成相应权限的数据
- 'topPriName' => '', // 顶级权限的名称'digui'=> 0, // 是否无限级(递归)'diguiName'=>'', // 递归时用来显示的字段的名字, 如 cat_name(分类名称)
- 'pk' => 'id', // 表中主键字段名称
- /********************* 要生成的模型文件中的代码 ******************************/
- // 添加时允许接收的表单中的字段
- 'insertFields' => "array('level_name','jifen_bottom','jifen_top')",
- // 修改时允许接收的表单中的字段
- 'updateFields' => "array('id','level_name','jifen_bottom','jifen_top')",
- 'validate' => "array('level_name','require','级别名称不能为空!', 1,'regex', 3),
- array('level_name', '1,30', '级别名称的值最长不能超过 30 个字符!', 1, 'length', 3),
- array('jifen_bottom', 'require', '积分下限 不能为空!', 1, 'regex', 3),
- array('jifen_bottom', 'number', '积分下限 必须是一个整数!', 1, 'regex', 3),
- array('jifen_top', 'require', '积分上限 不能为空!', 1, 'regex', 3),
- array('jifen_top', 'number', '积分上限 必须是一个整数!', 1, 'regex', 3),
- ",
- /********************** 表中每个字段信息的配置 ****************************/
- 'fields' => array(
- 'level_name' => array(
- 'text' => '级别名称',
- 'type' => 'text',
- 'default' => '',
- ),
- 'jifen_bottom' => array(
- 'text' => '积分下限',
- 'type' => 'text',
- 'default' => '',
- ),
- 'jifen_top' => array(
- 'text' => '积分上限',
- 'type' => 'text',
- 'default' => '',
- ),
- ),
- /**************** 搜索字段的配置 **********************/
- 'search' => array(
- ),
- );
- ?>
选择生成代码, 输入: p39_member_level.PHP
验证
http://localhost:8989/PHP/TpShop/admin/MemberLevel/lst
http://localhost:8989/PHP/TpShop/admin/MemberLevel/add
第三步 在添加商品时可以设置每个级别的价格
- public function add(){
- if(IS_POST){
- $model =D('goods');
- if( $model->create(I('post.'),1)) {
- if($model->add()){
- // 插入到数据库
- $this->success('操作成功',U('lst'));
- exit;
- //http://localhost:8989/PHP/TpShop/Admin/goods/add
- }
- }
- $error=$model->getError();
- $this->error($error);
- }
- // 显示表单
- // 取出所有会员级别
- $mlModel=D('member_level');
- $mlData=$mlModel->select();
- // 取出所有的品牌
- // $brandModel=D('brand');
- //$brandData=$brandModel->select();
- // print_r($brandData);
- //
- $this->assign(array(
- // 'brandData'=>$brandData,
- 'mlData'=>$mlData,
- '_page_title'=>'添加商品',
- '_page_btn_name'=>'商品列表',
- '_page_btn_link'=>U('lst'),
- ));
- $this->display();
- }
add.html
- <td>
- <?PHP foreach ($mlData as $k=>
- $v):?>
- <?PHP echo $v[ 'level_name'];?>
- :¥
- <input type="text" name="shop_price" value="0" size="8" />
- 元
- < br/>
- <?PHP endforeach;?>
- </td>
- </tr>
测试代码:
- if(IS_POST){
- var_dump($_POST);
- die;
- $model =D('goods');
- if( $model->create(I('post.'),1)) {
- if($model->add()){
- // 插入到数据库
- $this->success('操作成功',U('lst'));
- exit;
- //http://localhost:8989/PHP/TpShop/Admin/goods/add
- }
- }
打印出来的结果:
array(7) { ["brand_id"]=> string(1) "1" ["goods_name"]=> string(12) "iphoneXS Max" ["market_price"]=> string(4) "8399" ["shop_price"]=> string(4) "8399" ["member_price"]=> string(5) "20000" ["is_on_sale"]=> string(3) "鏄" ["goods_desc"]=> string(0) "" }
["member_price"]=> string(5) 不对
需要改一下会员名称:
- <tr>
- <td class="label">
- 会员价格:
- </td>
- <td>
- <?PHP foreach ($mlData as $k=>
- $v):?>
- <?PHP echo $v[ 'level_name'];?>
- :¥
- <input type="text" name="member_price[]" value="0" size="8" />
- 元
- < br/>
- <?PHP endforeach;?>
- </td>
- </tr>
输出结果:
array(7) { ["brand_id"]=> string(1) "1" ["goods_name"]=> string(15) "Apple iPhone XR" ["market_price"]=> string(4) "1340" ["shop_price"]=> string(4) "1340" ["member_price"]=> array(4) { [0]=> string(1) "0" [1]=> string(4) "1000" [2]=> string(5) "10000" [3]=> string(5) "20000" } ["is_on_sale"]=> string(3) "鏄" ["goods_desc"]=> string(0) "" }
修改一下级别 id
- <?PHP foreach ($mlData as $k=>$v):?>
- <?PHP echo $v['level_name'];?>:¥ <input type="text" name="member_price[<?php echo $v['id'];?>]" value="0" size="8"/>元 < br/>
- <?PHP endforeach;?>
- </td>
思考问题方法:
在 goodsModel 模型类加入
- protected function _after_insert($data,$option){
- $mp=I('post.member_price');
- /*
- 获取 < input type="text" name="member_price[<?php echo $v['id'];?>]" value="0" size="8"/>中的值
- */
- $mpModel=D('member_price');
- foreach ($mp as $k=>$v){
- $_v=(float)$v;
- if($_v>0){
- $mpModel->add(array(
- 'price'=>$v,
- 'level_id'=>$k,
- 'goods_id'=>$data['id'],
- ));
- }
- }
- }
来源: http://www.jianshu.com/p/0083e0d302f0