[PHP] 商品類型規格屬性後臺管理(代碼流程備忘)

来源:http://www.cnblogs.com/taoshihan/archive/2016/05/05/5462638.html
-Advertisement-
Play Games

實現界面 涉及到四張表,type(商品類型表),type_spec(商品類型規格關聯表),attribute(商品屬性表),attribute_value(商品屬性值表) 新建基控制器BaseController.class.php,向上抽取出來的公用方法 BaseController.class. ...


實現界面

 

涉及到四張表,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);
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • UDP特點: 面向無連接,把數據打包發過去,收不收得到我不管 數據大小有限制,一次不能超過64k,可以分成多個包 這是個不可靠的協議 速度很快 視頻直播,凌波客戶端,feiQ都是UDP協議 TCP特點: 面向連接,對方必須在 三次握手完成連接,我:在嗎;你:我在;我:我知道了 大數據量傳輸 速度稍慢 ...
  • 網路通信的步驟, 1.找到對方的ip 2.數據發送到對方指定的應用程式上,為了標識這些應用程式,用數字進行標識,這個數字就是埠 3.定義通信規則,這個規則就稱為協議 國際組織定義了通用協議 TCP/IP 網路模型 OSI參考模型 網路分成7層,應用層 ==> 表示層 ==> 會話層 ==> 傳輸層 ...
  • 今天我們來介紹一下C語言操作資料庫的方法,這裡我們使用的是ODBC方式。環境是WIN7+VC6。其他環境也差不多,具體情況具體分析。 首先是環境的配置以及數據源的添加。這裡就不去解釋了,相關資料網上有很多。需要註意的是這裡不可以直接使用控制面板中的ODBC,我們需要打開C:\Windows\SysW ...
  • import java.util.ArrayList;import java.util.Scanner; /* * 題目描述: * 讀入數據string[ ],然後讀入一個短字元串。要求查找string[ ]中和短字元串的所有匹配,輸出行號、匹配字元串。 * 匹配時不區分大小寫,並且可以有一個用中括 ...
  • 全文純手打,不喜勿噴~ OOP封裝 //封裝一個類:用來管理普通人的特性和行為 //1、類的概念:人類在認識客觀世界時,發現某些事物具有共同特性,共同結構,共同行為。 // 為了方便,我們就將它們集合起來,並抽象出一個概念類管理它們,這個概念就是類。 //2、類的組成:標準類由:實例變數、構造器、設 ...
  • 一、遞歸函數 概念:遞歸演算法是一種直接或者間接的調用自身演算法的過程。在電腦編寫程式中,遞歸演算法對解決一大類問題是十分有效的。 特點: ①遞歸就是在過程或者函數里調用自身。 ②在使用遞歸策略時,必須有一個明確的遞歸條件,稱為遞歸出口。 ③遞歸演算法解題通常顯得很簡潔,但遞歸演算法解題的效率較低。所以一般 ...
  • 問題? Java7新增了關於文件屬性信息的一些新特性,通過java.nio.file.*包下麵的類可以實現設置或者讀取文件的元數據信息(比如最後修改時間,創建時間,文件大小,是否為目錄等等)。尤其是UserDefinedFileAttributeView,可以用來自定義文件的元數據信息。於是在自己的 ...
  • 1. 初識解釋器ghci 1.1 查看幫助: :? 1.2 修改提示符: :set prompt ghci>>> 1.3 加自己指定模塊: :module + Data.Ratio 2. 基本交互 2.1 基本算術運算 中綴表達式: 首碼表達式: 2.2 算術中的負數 -8其實並不是直接表示負數8, ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...