• 20150414--商品,属性,图片入库


    image

    目录

    一、商品管理 1

    1、建表的考虑 1

    2、添加商品 3

    3、上传图片生成缩略图 5

    4、封装上传函数,并生成缩略图。 6

    5、商品信息入库 8

    6、取出商品属性 9

    7、商品属性入库 11

    一、商品管理

    1、建表的考虑

    goods_thumb表示小图,100*100的,在列表页面中使用

    wps4EE0.tmp

    商品的中图goods_img在商品的详情页面中使用。

    wps4F00.tmp

    使用is_best表示是一个精品,使用is_new是新品 is_hot是热卖

    wps4F11.tmp

    建立商品表:

    create table it_goods(

          id smallint unsigned primary key auto_increment,

          goods_name varchar(32) not null comment '商品的名称',

          cat_id  smallint unsigned not null comment '商品所属栏目的id',

          goods_type tinyint unsigned not null comment '商品类型的id',

          goods_sn  varchar(32) not null comment '商品的货号',

          shop_price decimal(9,2) not null default 0.0 comment '商品的本店价格',

          market_price decimal(9,2) not null default 0.0 comment '商品的市场价格',

          goods_number smallint  not null default 0 comment '商品的库存',

          seotitle varchar(32) not null default '' comment 'seo标题,利用搜索引擎的',

          goods_desc varchar(128) not null default '' comment '商品的描述',

          goods_ori varchar(64)  not null default ''  comment '商品的原图',

          goods_thumb varchar(64) not null default '' comment '商品的小图',

          goods_img varchar(64) not null default '' comment '商品的中图',

          is_best tinyint  not null default 0  comment '是否是精品,1表示为精品,0表示不是',

          is_new tinyint  not null default 0  comment '是否是新品,1表示为新品,0表示不是',

          is_hot tinyint  not null default 0  comment '是否是热卖,1表示为热卖,0表示不是',

          is_sale tinyint  not null default 0  comment '是否上架,1表示为上架销售,0表示不是',

        add_time int not null default 0 comment '商品的添加时间'

    )engine myisam charset utf8;

    需要建表完成属性的保存,如何建立表的字段

    goods_id(商品的id)  attr_id(属性的id)  attr_value(属性的值)   

    wps4F21.tmp

    wps4F32.tmp

    比如存储:

    goods_id       attr_id     attr_value

    1              1          黑白金

    2              5             

    #商品属性表

    create table it_goods_attr(

             id smallint unsigned primary key auto_increment,

             goods_id smallint not null comment '商品的id',

             attr_id tinyint unsigned not null  comment '存储属性的id',

             attr_value varchar(10) not null comment '属性的值'

    )engine myisam charset utf8;

    2、添加商品

    (1)在goods模块下面新建一个goods的控制器,并添加add方法,并拷贝对应的模板页面。

    (2)修改模板页面里面添加商品的表单。主要是添加商品属性的选项卡

    wps4F43.tmp

    第一步:添加商品属性的标签

    wps4F44.tmp

    第二步:添加商品属性的具体的内容

    wps4F54.tmp

    第三步:修改charea()函数

    wps4F65.tmp

    完成表单其他信息的修改,要注意表单里面的名称要和表中的字段名称一致。

    wps4F85.tmp

    (3)在控制器中,完成添加商品的代码

    wps4F96.tmp

    (4)新建一个goods模型,添加_before_insert钩子函数

    钩子函数有6个

    _before_insert(&$data,$option)是$model->add()函数触发_before_insert和_after_insert函数。

    _after_insert($data,$option)

    _before_update()

    _after_update()

    _before_delete()

    _after_delete()

    3、上传图片生成缩略图

    在goods的模型里面添加_before_insert函数,该函数完成图片上传。

    wps4FA6.tmpwps4FA7.tmpwps4FC8.tmp

    上传成功后,返回的信息。

    wps4FD8.tmp

    4、封装上传函数,并生成缩略图。

    自定义函数可以在common目录下面的common目录下面新建一个function.php文件。在该文件中定义的函数,可以在控制器和模型里面直接使用。

    wps4FE9.tmp

    wps5009.tmp

    wps501A.tmp

    wps502A.tmp

    最后,模型里面_before_insert的代码

    wps503B.tmp

    5、商品信息入库

    在goods模型中添加数据验证的信息

    wps504C.tmp

    控制器入库的代码:

    wps505C.tmp

    6、取出商品属性

    (1)在goods控制器中add方法中取出商品的类型

    wps506D.tmp

    wps507E.tmp

    表单里面有输入框,有下拉列表。

    如果是唯一属性,并且是手工录入,则生成一个输入框

    如果是唯一属性,并且是列表选择,则生成一个下拉列表

    如果是单选属性,并且是手工录入,则生成一个输入框

    如果是单选属性,并且是列表选择,则生成一个下拉列表,并前面添加 “[+]”表示自己能够复制。

    attr_type=0表示唯一属性  attr_type=1表示单选属性

    attr_input_type=0表示手工录入,attr_input_type=1表示列表选择

    在add.html页面中使用ajax来完成:

    wps508E.tmp

    根据ajax完成在控制器里面添加showttr方法。

    wps509F.tmp

    showattr.html页面里面内容:

    wps50BF.tmp

    wps50D0.tmp

    wps5100.tmp

    7、商品属性入库

    入 it_goods_attr表;

    wps5110.tmp

    商品属性入库是在商品入库后再执行的。因此要写到 _after_insert钩子函数中

    分析属性提交过来的数据

    wps5111.tmpwps5122.tmpwps5132.tmp

    wps5153.tmp

    8、完成商品列表取出

  • 相关阅读:
    Spring boot核心注解
    Spring-boot配置文件
    JAVA操作Excel
    操作系统之基础
    Batch Normalization
    解决ios微信页面回退不刷新的问题
    textarea高度自适应,随着内容增加高度增加
    旋转卡 可以用做登录注册
    一个页面tab标签切换,都有scroll事件的解决办法
    input type="radio" 赋值问题
  • 原文地址:https://www.cnblogs.com/lifushan/p/5472154.html
Copyright © 2020-2023  润新知