实现界面

涉及到四张表,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. 基于JMS的ActiveMQ搭建与实现

    1.JMS Java消息服务(Java Message Service)即JMS,是一个Java平台中关于面向消息中间件的API,用于两个程序之间,或分布式系统中发送消息,进行异步通信. JMS包括队 ...

  2. js 从基础入门 到放弃 001

    快速入门  JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <html> <head> <s ...

  3. 消息队列 MQ 入门理解

    功能特性: 应用场景: 消息队列 MQ 可应用于如下几个场景: 分布式事务 在传统的事务处理中,多个系统之间的交互耦合到一个事务中,响应时间长,影响系统可用性.引入分布式事务消息,交易系统和消息队列之 ...

  4. 表格(table)

    <table border="1"; align="center" cellspacing="0"> <tr> &l ...

  5. iOS多线程---NSOperation介绍和使用

    1.  NSOperation实现多线程编程,需要和NSOperationQueue一起使用. (1)先将要执行的操作封装到NSOperation中 (2)将NSOperation对象添加到NSOpe ...

  6. 页面按钮埋点+跟踪location.search

    <a href="javascript: void(0)" onclick="setUrl('https://baoxian.pingan.com/pa18shop ...

  7. Mac 下安装node.js(转载)

    原文地址:http://blog.csdn.net/u011619283/article/details/52368759 Node.js 简介 Node.js 是一个用Chrome's V8 Jav ...

  8. 【树】Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...

  9. 《LeetBook》leetcode题解(15):3Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  10. golang prometheus包的使用

    prometheus包提供了用于实现监控代码的metric原型和用于注册metric的registry.子包(promhttp)允许通过HTTP来暴露注册的metric或将注册的metric推送到Pu ...