TP电商项目:商品会员级别

需求分析:

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'],




            ));

            }

        }




    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容