CI Framework implementation of shopping cart functional steps

Source: Internet
Author: User
Tags set cookie
This time to bring you a CI framework to achieve the shopping cart functional steps in detail, the CI framework to realize the shopping cart function of the note what, the following is the actual case, a look.

In the Mall project, shopping cart is a very important link, here leave the source code, leave notes!!!

Words don't say much, look down:

1. Source Code

<?phpdefined (' BasePath ') or exit (' No Direct script access allowed '); class Cart extends Home_controller {private $info    = Array ();  #前台提交数据 Private $specData = Array ();  #规格信息 Private $prodData = Array ();  #货品组合信息 Private $cartData = Array ();    #购物车入库数据/** * Constructor/Public function construct () {parent::construct ();    $this->load->model (' Goodsmodel ', ' goods ');    $this->load->model (' ProductModel ', ' product ');  $this->load->model (' Goodsattrmodel ', ' goodsattr ');  }/** * [Shopping cart] Data add */Public Function Cartadd () {#接收购物车提交数据 $this->info = $this->input->post ();    $this->ajaxreturn ($this->info);    #1. Verify product Inventory, Inventory $this->checkgoodsnumber ();    #2. Query specification name, Price $this->getspecdata ();  #3. Assembling the shopping cart add de data $cartData = $this->setcartdata ();    P (Json_decode ($this->input->cookie (' cart '), true)); # One, determine if login if (! UID) {//No login data is stored in cookie//1: Get cart data in cookie $cookieCartData = $this->input->cookie (' CArt ');        2: Determine if the data in the cookie is empty if (empty ($cookieCartData)) {//2-1: empty means that the user has not added a shopping cart//2-1-1. Set up key--> to generate shopping cart data $key = $cartData [' goods_id ']. '        -'. $cartData [' product_id '];        $cookieCart = Array ($key = = $cartData);        2-1-2. Set Shopping Cart return value (item quantity, Total Price) $this->setcartreturn (1, $cartData [' Goods_price ']); 2-1-3. Setting cookies to store shopping cart data}else{//2-2: NOT NULL indicates that the user has added a shopping cart//2-2-1. Append shopping Data $cookieCart = $this-&GT;ADDCA        Rtdata ($cartData, Json_decode ($cookieCartData, true)); 2-2-2. Set Shopping Cart return value (item quantity, Total Price) $this->setcartreturn (count ($cookieCart), Array_sum (Array_column ($cookieCart, ' Goods_      Price '));    }//3: Set cookie to store cart data setcookie (' Cart ', Json_encode ($cookieCart), left_time, '/');  }else{//Login data into Database}//return shopping cart hint data $this->ajaxreturn ($this->msg); }/** * Verify commodity inventory */Public function Checkgoodsnumber () {$this->goods->map = array (' goods_id ' =&gt  ; $this->info[' goods_id '], ' gOods_number >= ' = $this->info[' buy_number ');    $this->goods = $this->goods->find (' Goods_id,goods_name,goods_sn,goods_img,shop_price ');      if (! $this->goods) {$this->msg[' msg '] = "Insufficient inventory of goods";    $this->ajaxreturn ($this->msg); } #验证货品库存 $this->product->map = Array (' goods_id ' = = $this->info[' goods_id '), ' product_at    Tr ' = = $this->info[' prod_attr '], ' product_number >= ' + $this->info[' Buy_number '],);    $this->proddata = $this->product->find ();      if (! $this->proddata) {$this->msg[' msg '] = "Insufficient inventory of goods";    $this->ajaxreturn ($this->msg);  } return true; }/** * Combination specification Name, price */Public Function Getspecdata () {$this->goodsattr->map = Intotype (Explode ("|", $this-&    gt;info[' prod_attr '), ' goods_attr_id ');    $goodsAttrInfo = $this->goodsattr->select (' Goods_attr_value,goods_attr_price '); $this->specdata[' product_attr_value ' = Implode ("|", ARRay_column ($goodsAttrInfo, ' goods_attr_value '));    $this->specdata[' product_price ') = Array_sum (Array_column ($goodsAttrInfo, ' goods_attr_price '));      # Return specification information $this->specdata}/** * Assembly cart added array */Public function Setcartdata () {$this->cartdata = array (      ' product_id ' = $this->proddata[' product_id '], ' product_attr ' + $this->proddata[' product_attr '], ' Buy_number ' + $this->info[' buy_number '], ' goods_price ' + $this->info[' shop_price '], ' goo Ds_sum ' = = $this->info[' shop_price ') * $this->info[' buy_number '], ' product_price ' = ', ' Produc    T_attr_value ' = = ', ' uid ' = uid,);    $this->cartdata = Array_merge ($this->cartdata, $this->goods); #若存在规格 "Add specification Information" if (!empty ($this->info[' prod_attr ')) {$this->cartdata[' product_price '] = $this->specdata['      Product_price ']; $this->cartdata[' product_attr_value ') = $this->specdata[' Product_attr_valUE '];    } return $this->cartdata;  # total data added by shopping cart $this->cartdata; }/** * Set cart return prompt data * @param [number of items, Total price] */Public Function Setcartreturn ($number, $prices) {$this->msg[' code    '] = self::status_on;  $this->msg[' data ' = Array (' number ' = = $number, ' prices ' = = $prices,); */** * Shopping Cart new Add data * @param [new data, original cart data] */Public Function Addcartdata ($newData, $oldData) {#组合Key $key = $ newdata[' goods_id ']. '    -'. $newData [' product_id ']; #判断购物车中是否有该商品 if (isset ($oldData [$key])) {//1. There is a combined quantity of goods, price $oldData [$key] [' buy_number '] = $oldData [$key] [' BU      Y_number '] + $newData [' buy_number '];      $oldData [$key] [' goods_price '] = $newData [' Goods_price '];    $oldData [$key] [' goods_sum '] = $oldData [$key] [' buy_number '] * $oldData [$key] [' Goods_price '];    }else{//2. No new product $oldData [$key] = $newData;  } #返回购物车数据 return $oldData; }}?>

2. Database

CREATE TABLE ' shop_goods ' (' goods_id ' int (ten) unsigned NOT null auto_increment, ' goods_name ' varchar (255) is not NULL, ' type ' _id ' int (one) default NULL, PRIMARY KEY (' goods_id ')) Engine=innodb auto_increment=10 default Charset=utf8; CREATE TABLE ' shop_product ' (' product_id ' int (one) unsigned not null auto_increment, ' goods_id ' int (one) not null, ' goods_p Rice ' Decimal (10,2) is not null, ' goods_num ' int (one) not null, ' GOODS_SN ' varchar (a) NOT null, ' goods_attr_id ' varchar (100) Not NULL, PRIMARY KEY (' product_id ')) Engine=innodb auto_increment=18 DEFAULT Charset=utf8; CREATE TABLE ' shop_goods_attr ' (' goods_attr_id ' int (ten) unsigned NOT null auto_increment, ' goods_id ' int (one) not NULL, ' a ttr_id ' int (one) not null, ' attr_value ' varchar (255) is not NULL, PRIMARY KEY (' goods_attr_id ')) Engine=innodb auto_increment= 126 DEFAULT Charset=utf8;

CI Shopping cart summary finished!!!

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP implementation of large file cutting and merging steps

PHP Group Word algorithm implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.