实现界面

涉及到四张表,type(商品类型表),type_spec(商品类型规格关联表),attribute(商品属性表),attribute_value(商品属性值表)

新建基控制器BaseController.class.php,向上抽取出来的公用方法

BaseController.class.php

<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
protected $pageSize=1;
/**
* 获取分页对象
* @param [type] $count [description]
* @return [type] [description]
*/
public function getPager($count){
$pager=new \Common\Libs\MyPage($count,$this->pageSize);
return $pager;
}
}

定义基模型文件BaseModel.class.php,继承系统的Model类

BaseModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class BaseModel extends Model{
/**
* 获取分页数据
* @param [type] $pager [description]
* @param array $condition [description]
* @param string $order [description]
* @return [type] [description]
*/
public function getPagerResult($pager,$condition=array(),$order=""){
if($pager==null){
return;
}
return $this->where($condition)
->limit($pager->firstRow.','.$pager->listRows)
->order($order)
->select();
}
/**
* 获取条数
* @return [type] [description]
*/
public function getCount($condition=array()){
return $this->where($condition)->count();
}
/**
* 添加数据
* @return [type] [description]
*/
public function addItem($data){
$msg=array();
if(!$this->create($data)){
$msg['msg']=$this->getError();
$msg['status']=false;
}else{
$id=$this->add($data);
if($id){
$msg['status']=true;
$msg['id']=$id;
}else{
$msg['status']=false;
$msg['msg']=$this->getError();
}
}
return $msg;
}
/**
* 获取单条数据
* @return [type] [description]
*/
public function getItem($condition=array()){
return $this->where($condition)->find();
}
/**
* 获取所有数据
* @return [type] [description]
*/
public function getAllResult($condition=array()){
return $this->where($condition)->select();
}
/**
* 删除数据
* @return [type] [description]
*/
public function delItem($condition=array()){
if(empty($condition)){
return false;
}
return $this->where($condition)->delete();
}
/**
* 编辑数据
* @return [type] [description]
*/
public function setItem($condition=array(),$data){
if(empty($condition)){
return false;
}
$msg=array();
if(!$this->create($data)){
$msg['msg']=$this->getError();
$msg['status']=false;
}else{
$id=$this->where($condition)->save($data);
$msg['status']=true;
$msg['id']=$id;
}
return $msg;
}
}

新建类型控制器文件TypeController.class.php

TypeController.class.php

<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 类型(规格,属性)
*/
class TypeController extends BaseController {
private $typeModel;
private $specModel;
private $attrModel;
private $typeId;
public function __construct(){
parent::__construct();
$this->typeModel=D("Type");
$this->specModel=D("Spec");
$this->attrModel=D("Attr");
}
/**
* 列表
* @return [type] [description]
*/
public function index(){
$nums=$this->typeModel->getCount();
$pager=$this->getPager($nums);
$typeList=$this->typeModel->getPagerResult($pager,array(),"type_id desc");
$pageHtml=$pager->show(); $this->assign('typeList',$typeList);
$this->assign('pageHtml',$pageHtml);
$this->display();
}
/**
* 添加类型(添加进4张表 type,type_spec,attribute,attribute_value)
* @return [type] [description]
*/
public function addType(){
if(IS_POST){
//添加类型表
$res=$this->typeModel->addTypeItem($_POST);
if($res['status']){
$this->typeId=$res['id'];
//添加属性
if($_POST['attr_value'][0]['name']){
$this->addAttrData($_POST['attr_value']);
}
$this->success("操作成功!");
}else{
$this->error($res['msg']);
}
}else{
$specList=$this->specModel->select();
$this->assign("specList",$specList);
$this->display();
}
}
/**
* 添加属性
* @param [type] $data [description]
*/
private function addAttrData($data){
foreach ($data as $key => $value) {
$temp=array();
$temp['attr_name']=$value['name'];
$temp['attr_values']=$value['value'];
$temp['type_id']=$this->typeId;
$this->attrModel->addAttrItem($temp);
}
return true;
}
/**
* 编辑属性
* @param [type] $data [description]
*/
private function setAttrData($data){
foreach ($data as $key => $value) {
$temp=array();
$temp['attr_id']=$key;
$temp['attr_name']=$value['name'];
$temp['attr_values']=$value['value'];
$temp['type_id']=$this->typeId;
$this->attrModel->setAttrItem($temp);
//添加属性
if($key=='new'){
$this->addAttrData($value);
break;
}
}
return true;
}
/**
* 编辑类型(添加进4张表 type,type_spec,attribute,attribute_value)
* @return [type] [description]
*/
public function editType(){
$typeId=intval($_GET['typeId']); if(IS_POST){
$this->typeId=intval($_POST['type_id']);
//编辑类型表
$res=$this->typeModel->editTypeItem($_POST);
if($res['status']){
//编辑属性
if(!empty($_POST['attr_value'])){
$this->setAttrData($_POST['attr_value']);
}
$this->success("操作成功!",U("Type/editType",array('typeId'=>$this->typeId)));
}else{
$this->error($res['msg']);
}
}else{
$typeInfo=$this->typeModel->getItem(array('type_id'=>$typeId));
$this->assign("typeInfo",$typeInfo); $specList=$this->specModel->select();
$this->assign("specList",$specList); $specTypeList=M("type_spec")->where(array('type_id'=>$typeId))->getField("spec_id",true);
$this->assign("specTypeList",$specTypeList); $attrList=$this->attrModel->getAllResult(array('type_id'=>$typeId));
$this->assign("attrList",$attrList); $this->display();
}
}
}

新建类型模型文件TypeModel.class.php

TypeModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class TypeModel extends BaseModel{
/**
* 验证规则
* @var array
*/
protected $_validate = array(
array('type_name','require','类型名称必须!')
);
/**
* 添加类型
*/
public function addTypeItem($data){
$res=$this->addItem($data); //添加类型规格
$typeSpecs=$data['spec_id'];
$typeSpecArray=array();
foreach ($typeSpecs as $typeSpec) {
$temp=array();
$temp['type_id']=$res['id'];
$temp['spec_id']=$typeSpec;
$typeSpecArray[]=$temp;
} M("type_spec")->addAll($typeSpecArray);
return $res;
}
/**
* 编辑类型
*/
public function editTypeItem($data){
$res=$this->setItem(array('type_id'=>$data['type_id']),$data); //编辑类型规格
M("type_spec")->where(array('type_id'=>$data['type_id']))->delete();
$typeSpecs=$data['spec_id'];
$typeSpecArray=array();
foreach ($typeSpecs as $typeSpec) {
$temp=array();
$temp['type_id']=$data['type_id'];
$temp['spec_id']=$typeSpec;
$typeSpecArray[]=$temp;
} M("type_spec")->addAll($typeSpecArray); return $res;
}
}

新建规格模型文件SpecModel.class.php

SpecModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class SpecModel extends BaseModel{
/**
* 验证规则
* @var array
*/
protected $_validate = array(
array('spec_name','require','规格名称必须!'),
array('spec_sort','number','排序必须是数字!')
);
}

新建属性模型文件AttrModel.class.php

AttrModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class AttrModel extends BaseModel{
protected $tableName="attribute";
/**
* 验证规则
* @var array
*/
protected $_validate = array(
array('attr_name','require','属性名称必须!'),
array('type_id','require','类型id必须!'),
array('attr_values','require','属性值必须!')
);
/**
* 添加属性
*/
public function addAttrItem($data){
$res=$this->addItem($data); //添加属性值
$attrValues=explode("|", $data['attr_values']);
$attrValueArray=array();
foreach ($attrValues as $attrValue) {
$temp=array();
$temp['attr_id']=$res['id'];
$temp['attr_value']=$attrValue;
$attrValueArray[]=$temp;
} M("attribute_value")->addAll($attrValueArray);
}
/**
* 编辑属性
*/
public function setAttrItem($data){
$res=$this->setItem(array('attr_id'=>$data['attr_id']),$data); //编辑属性值
M("attribute_value")->where(array('attr_id'=>$data['attr_id']))->delete();
$attrValues=explode("|", $data['attr_values']);
$attrValueArray=array();
foreach ($attrValues as $attrValue) {
if(!$attrValue){
break;
}
$temp=array();
$temp['attr_id']=$data['attr_id'];
$temp['attr_value']=$attrValue;
$attrValueArray[]=$temp;
} M("attribute_value")->addAll($attrValueArray);
}
}

[PHP] 商品类型规格属性后台管理(代码流程备忘)的更多相关文章

  1. [开源] FreeSql.AdminLTE.Tools 根据实体类生成后台管理代码

    前言 FreeSql 发布至今已经有9个月,功能渐渐完善,自身的生态也逐步形成,早在几个月前写过一篇文章<ORM 开发环境之利器:MVC 中间件 FreeSql.AdminLTE>,您可以 ...

  2. C#常用代码片段备忘

    以下是从visual studio中整理出来的常用代码片段,以作备忘 快捷键: eh 用途: 类中事件实现函数模板 private void MyMethod(object sender, Event ...

  3. httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!

    2个类,一个基类,一个构建头信息调用类 关于如何获取到post中的内容,你之需要用http抓包工具把你与目标网站的请求信息抓下来后,打开分析下按照抓下来的包中的数 据进行构建就行了 using Sys ...

  4. 玩转html5(一)-----盘点html5新增的那些酷酷的input类型和属性

    今天正式开始学习html5了,相比html以前的版本,html5新增了好多功能,属性,使我们做出来的界面更加的绚丽,而且使用起来超级简单,这篇文章先来说说html增加的那些input类型和属性. 这些 ...

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

    思路: 1.不同商品属于不同的类型,如:手机.服装.电脑等类型 2.不同的类型有不同的属性,其中分为唯一属性和可选属性,如服装:可选属性{尺寸:S,M,L……;颜色:白色,黑色……}唯一属性:材质 首 ...

  6. magento -- 给后台分类管理页的分类商品加一栏商品类型

    当使用特定分类来控制前台的商品显示时,后台分类管理页的分类商品只有编号.名称.SKU和价格这几栏,选择特定商品相当不便. 可以在这里多加一栏商品类型用来筛选商品,添加的方式很简单. 打开文件/app/ ...

  7. 微擎 人人商城 对接京东vop 对接京东商品,同步商品 地址,库存,价格,上下架等。(二) 设置后台管理界面

    昨天提到了,由于vop商品池未开通,故对接工作只能暂缓,现在要做一个专门针对vop商品的后台管理, 老规矩,先上设计链路图 因为后台本来就是有比较完善的商品管理系统, 所以我们只是针对vop 进行简单 ...

  8. 商品类型的下拉框绑定一个事件,通过ajax获取属性

    html代码这么写 <!-- 商品属性 --> <table cellspacing="1" cellpadding="3" width=&q ...

  9. zencart后台管理中选项名称和选项内容和属性控制页面出错解决办法 WARNING: An Error occurred, please refresh the page and try again

    后台管理中选项名称和选项内容和属性控制出现以下错误的解决办法WARNING: An Error occurred, please refresh the page and try again zen ...

随机推荐

  1. CEPH 创建 RPM 安装包

    1.安装依赖环境 yum install gcc make python-devel openssl-devel graphviz autoconf automake rpm-build redhat ...

  2. 975. Odd Even Jump

    You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1 ...

  3. jvm特性(3)( 收集算法和收集器的概念)

    java内存模型和线程规范 JVM高级特性与实践(三):垃圾收集算法 与 垃圾收集器实现 大致知识点如下: 4种垃圾收集算法概念的学习 7种垃圾收集器特征的学习 一. 垃圾收集算法 1. 标记-清除算 ...

  4. 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

    引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...

  5. 安装Nginx并为node.js设置反向代理

    最近看了反向代理和正向代理的东西,想到自己的node.js服务器是运行在3333端口的,也没有为他设置反向代理,node.js项目的一些静态文件是完全可以部署在Nginx上,以减少对node.js的请 ...

  6. vue-Treeselect 使用备注

    <head> <!-- include Vue 2.x --> <script src="https://cdn.jsdelivr.net/npm/vue@@^ ...

  7. eolinker接口测试平台的安装部署

    1.从GitHub下载安装包: https://github.com/eolinker/CHN-EOLINKER-AMS-Lite-4.0-For-Java 使用 git clone https:// ...

  8. MySQL命令行导入sql文件时出现乱码解决方案

    Note: sql> source F:weibo.sql(执行相关sql文件) sql> select * from sina into outfile "/weibo.txt ...

  9. (转)Mysql 索引原理及优化

    本文内容主要来源于互联网上主流文章,只是按照个人理解稍作整合,后面附有参考链接. 一.摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引 ...

  10. Java之IO(一)InputStream和OutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6964702.html 1.前言 计算机的IO操作一直都是比较重要的一环,IO顾名思义,就是输入输出流.不管是磁盘 ...