濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車功能的方法

基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車功能的方法

熱門標(biāo)簽:長(zhǎng)沙做地圖標(biāo)注公司 福建銀行智能外呼系統(tǒng)價(jià)格 房產(chǎn)中介用的是什么外呼系統(tǒng) 電話機(jī)器人銷售主要負(fù)責(zé)什么 寧波外呼營(yíng)銷系統(tǒng) 地圖標(biāo)注專員怎么樣 遼寧ai電銷機(jī)器人價(jià)格 四川保險(xiǎn)智能外呼系統(tǒng)供應(yīng)商 上海做外呼線路的通信公司

本文實(shí)例講述了基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車功能的方法。分享給大家供大家參考,具體如下:

在商城項(xiàng)目中,購(gòu)物車是非常重要的一環(huán),此處留下源碼,留作筆記!?。?/p>

話不多說(shuō),往下看:

1. 源代碼

?php
defined('BASEPATH') OR exit('No direct script access allowed');
class cart extends Home_Controller {
  private $info = array();    #前臺(tái)提交數(shù)據(jù)
  private $specData = array();  #規(guī)格信息
  private $prodData = array();  #貨品組合信息
  private $cartData = array();  #購(gòu)物車入庫(kù)數(shù)據(jù)
  /**
   * 構(gòu)造函數(shù)
   */
  public function __construct()
  {
    parent::__construct();
    $this->load->model('goodsModel','goods');
    $this->load->model('productModel','product');
    $this->load->model('goodsAttrModel','goodsAttr');
  }
  /**
   * [購(gòu)物車]數(shù)據(jù)添加
   */
  public function cartAdd()
  {
    #接收購(gòu)物車提交數(shù)據(jù)
    $this->info = $this->input->post();
  // $this->ajaxReturn($this->info);
    #1.驗(yàn)證商品庫(kù)存、貨品庫(kù)存
    $this->checkGoodsNumber();
    #2.查詢規(guī)格名稱、價(jià)格
    $this->getSpecData();
    #3.組裝購(gòu)物車添加de數(shù)據(jù)
    $cartData = $this->setCartData();
  p(json_decode($this->input->cookie('cart'),true));
    # 一、判斷是否登錄
    if(!UID){
      //未登錄 數(shù)據(jù)存入Cookie中
      //1:獲取cookie中的購(gòu)物車數(shù)據(jù)
      $cookieCartData = $this->input->cookie('cart');
      //2:判斷cookie中數(shù)據(jù)是否為空
      if(empty($cookieCartData)){
        //2-1:為空則表示用戶沒(méi)有添加過(guò)購(gòu)物車
        //2-1-1.設(shè)置Key-->生成購(gòu)物車數(shù)據(jù)
        $key = $cartData['goods_id'].'-'.$cartData['product_id'];
        $cookieCart = array($key => $cartData);
        //2-1-2.設(shè)置購(gòu)物車返回值(商品數(shù)量、總價(jià))
        $this->setCartReturn(1,$cartData['goods_price']);
        //2-1-3.設(shè)置Cookie存儲(chǔ)購(gòu)物車數(shù)據(jù)
      }else{
        //2-2:不為空 表示用戶添加過(guò)購(gòu)物車
        //2-2-1.追加購(gòu)物數(shù)據(jù)
        $cookieCart = $this->addCartData($cartData,json_decode($cookieCartData,true));
        //2-2-2.設(shè)置購(gòu)物車返回值(商品數(shù)量、總價(jià))
        $this->setCartReturn(count($cookieCart),array_sum(array_column($cookieCart, 'goods_price')));
      }
      //3:設(shè)置Cookie存儲(chǔ)購(gòu)物車數(shù)據(jù)
      setCookie('cart',json_encode($cookieCart),LEFT_TIME,'/');
    }else{
      //已登錄 數(shù)據(jù)存入數(shù)據(jù)庫(kù)
    }
    //返回購(gòu)物車提示數(shù)據(jù)
    $this->ajaxReturn($this->msg);
  }
  /**
   * 驗(yàn)證商品庫(kù)存
   */
  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'] = "商品庫(kù)存不足";
      $this->ajaxReturn($this->msg);
    }
    #驗(yàn)證貨品庫(kù)存
    $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'] = "貨品庫(kù)存不足";
      $this->ajaxReturn($this->msg);
    }
    return true;
  }
  /**
   * 組合規(guī)格名稱、價(jià)格
   */
  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'));
    # 返回規(guī)格信息 $this->specData
  }
  /**
   * 組裝購(gòu)物車添加的數(shù)組
   */
  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);
    #若存在規(guī)格【添加規(guī)格信息】
    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;
    # 購(gòu)物車 添加的總數(shù)據(jù) $this->cartData;
  }
  /**
   * 設(shè)置購(gòu)物車返回提示數(shù)據(jù)
   * @param [商品數(shù)量,總價(jià)]
   */
  public function setCartReturn($number,$prices)
  {
    $this->msg['code'] = self::STATUS_ON;
    $this->msg['data'] = array(
        'number'  =>  $number,
        'prices'  =>  $prices,
    );
  }
  /**
   * 購(gòu)物車 新添加數(shù)據(jù)
   * @param [新數(shù)據(jù),原購(gòu)物車數(shù)據(jù)]
   */
  public function addCartData($newData,$oldData)
  {
    #組合Key
    $key = $newData['goods_id'].'-'.$newData['product_id'];
    // #判斷購(gòu)物車中是否有該商品
    if(isset($oldData[$key])){
      //1.有 合并商品數(shù)量、價(jià)格
      $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.沒(méi)有 追加新商品
      $oldData[$key] = $newData;
    }
    #返回購(gòu)物車數(shù)據(jù)
    return $oldData;
  }
}
?>

2. 數(shù)據(jù)庫(kù)

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購(gòu)物車總結(jié)完畢?。?!

更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • php實(shí)現(xiàn)仿寫CodeIgniter的購(gòu)物車類
  • Codeigniter購(gòu)物車類不能添加中文的解決方法
  • 基于Codeigniter框架實(shí)現(xiàn)的student信息系統(tǒng)站點(diǎn)動(dòng)態(tài)發(fā)布功能詳解
  • CodeIgniter采用config控制的多語(yǔ)言實(shí)現(xiàn)根據(jù)瀏覽器語(yǔ)言自動(dòng)轉(zhuǎn)換功能
  • CI(CodeIgniter)框架中的增刪改查操作
  • CodeIgniter啟用緩存和清除緩存的方法
  • Codeigniter(CI)框架分頁(yè)函數(shù)及相關(guān)知識(shí)
  • CodeIgniter輔助函數(shù)helper詳解
  • Codeigniter注冊(cè)登錄代碼示例
  • Codeigniter實(shí)現(xiàn)處理用戶登錄驗(yàn)證后的URL跳轉(zhuǎn)

標(biāo)簽:宜春 澳門 延安 工商登記 佛山 宿遷 深圳 常德

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車功能的方法》,本文關(guān)鍵詞  基于,CodeIgniter,框架,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車功能的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車功能的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    泗水县| 洛阳市| 苍南县| 邢台市| 禹城市| 南靖县| 大冶市| 金乡县| 宁波市| 开封市| 固安县| 巴楚县| 凌云县| 上饶市| 深水埗区| 田林县| 台南县| 东辽县| 莒南县| 得荣县| 静乐县| 阿荣旗| 通州市| 潼南县| 衡东县| 井冈山市| 修武县| 射洪县| 长寿区| 武川县| 黄陵县| 郴州市| 遂昌县| 富川| 凤山县| 四子王旗| 南郑县| 肇东市| 吉水县| 遵义市| 英吉沙县|