這篇文章主要介紹了基於CI(CodeIgniter)架構實現購物車功能的方法,結合執行個體形式分析了CodeIgniter架構購物車功能類的定義及資料庫建立相關sql命令,需要的朋友可以參考下
本文執行個體講述了基於CI(CodeIgniter)架構實現購物車功能的方法。分享給大家供大家參考,具體如下:
在商城項目中,購物車是非常重要的一環,此處留下源碼,留作筆記!!!
話不多說,往下看:
1. 原始碼
<?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(); #購物車入庫資料 /** * 建構函式 */ public function __construct() { parent::__construct(); $this->load->model('goodsModel','goods'); $this->load->model('productModel','product'); $this->load->model('goodsAttrModel','goodsAttr'); } /** * [購物車]資料添加 */ public function cartAdd() { #接收購物車提交資料 $this->info = $this->input->post(); // $this->ajaxReturn($this->info); #1.驗證商品庫存、貨品庫存 $this->checkGoodsNumber(); #2.查詢規格名稱、價格 $this->getSpecData(); #3.組裝購物車添加de資料 $cartData = $this->setCartData(); p(json_decode($this->input->cookie('cart'),true)); # 一、判斷是否登入 if(!UID){ //未登入 資料存入Cookie中 //1:擷取cookie中的購物車資料 $cookieCartData = $this->input->cookie('cart'); //2:判斷cookie中資料是否為空白 if(empty($cookieCartData)){ //2-1:為空白則表示使用者沒有添加過購物車 //2-1-1.設定Key-->產生購物車資料 $key = $cartData['goods_id'].'-'.$cartData['product_id']; $cookieCart = array($key => $cartData); //2-1-2.設定購物車傳回值(商品數量、總價) $this->setCartReturn(1,$cartData['goods_price']); //2-1-3.設定Cookie儲存購物車資料 }else{ //2-2:不為空白 表示使用者添加過購物車 //2-2-1.追加購物資料 $cookieCart = $this->addCartData($cartData,json_decode($cookieCartData,true)); //2-2-2.設定購物車傳回值(商品數量、總價) $this->setCartReturn(count($cookieCart),array_sum(array_column($cookieCart, 'goods_price'))); } //3:設定Cookie儲存購物車資料 setCookie('cart',json_encode($cookieCart),LEFT_TIME,'/'); }else{ //已登入 資料存入資料庫 } //返回購物車提示資料 $this->ajaxReturn($this->msg); } /** * 驗證商品庫存 */ public function checkGoodsNumber() { $this->goods->map = array( 'goods_id' => $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'] = "商品庫存不足"; $this->ajaxReturn($this->msg); } #驗證貨品庫存 $this->product->map = array( 'goods_id' => $this->info['goods_id'], 'product_attr' => $this->info['prod_attr'], 'product_number >=' => $this->info['buy_number'], ); $this->prodData = $this->product->find(); if(!$this->prodData){ $this->msg['msg'] = "貨品庫存不足"; $this->ajaxReturn($this->msg); } return true; } /** * 組合規格名稱、價格 */ public function getSpecData() { $this->goodsAttr->map = inToType(explode("|", $this->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')); # 返回規格資訊 $this->specData } /** * 組裝購物車添加的數組 */ 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'], 'goods_sum' => $this->info['shop_price'] * $this->info['buy_number'], 'product_price' => '', 'product_attr_value' => '', 'uid' => UID, ); $this->cartData = array_merge($this->cartData,$this->goods); #若存在規格【添加規格資訊】 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; # 購物車 添加的總資料 $this->cartData; } /** * 設定購物車返回提示資料 * @param [商品數量,總價] */ public function setCartReturn($number,$prices) { $this->msg['code'] = self::STATUS_ON; $this->msg['data'] = array( 'number' => $number, 'prices' => $prices, ); } /** * 購物車 新添加資料 * @param [新資料,原購物車資料] */ public function addCartData($newData,$oldData) { #組合Key $key = $newData['goods_id'].'-'.$newData['product_id']; // #判斷購物車中是否有該商品 if(isset($oldData[$key])){ //1.有 合并商品數量、價格 $oldData[$key]['buy_number'] = $oldData[$key]['buy_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.沒有 追加新商品 $oldData[$key] = $newData; } #返回購物車資料 return $oldData; }}?>
2. 資料庫
CREATE TABLE `shop_goods` ( `goods_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `goods_name` varchar(255) NOT NULL, `type_id` int(11) DEFAULT NULL, PRIMARY KEY (`goods_id`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;CREATE TABLE `shop_product` ( `product_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `goods_id` int(11) NOT NULL, `goods_price` decimal(10,2) NOT NULL, `goods_num` int(11) NOT NULL, `goods_sn` varchar(50) 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(10) unsigned NOT NULL AUTO_INCREMENT, `goods_id` int(11) NOT NULL, `attr_id` int(11) NOT NULL, `attr_value` varchar(255) NOT NULL, PRIMARY KEY (`goods_attr_id`)) ENGINE=InnoDB AUTO_INCREMENT=126 DEFAULT CHARSET=utf8;
CI購物車總結完畢!!!
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!