基于CI(CodeIgniter)框架实现购物车功能的方法

 更新时间:2018年04月09日 12:04:48   作者:红尘炼炼心  
这篇文章主要介绍了基于CI(CodeIgniter)框架实现购物车功能的方法,结合实例形式分析了CodeIgniter框架购物车功能类的定义及数据库建立相关sql命令,需要的朋友可以参考下

本文实例讲述了基于CI(CodeIgniter)框架实现购物车功能的方法。分享给大家供大家参考,具体如下:

在商城项目中,购物车是非常重要的一环,此处留下源码,留作笔记!!!

话不多说,往下看:

1. 源代码

<?php
defined('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购物车总结完毕!!!

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

相关文章

  • Laravel等框架模型关联的可用性浅析

    Laravel等框架模型关联的可用性浅析

    这篇文章主要给大家介绍了关于Laravel等框架模型关联的可用性的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Laravel等框架具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • PHP之浮点数计算比较以及取整数不准确的解决办法

    PHP之浮点数计算比较以及取整数不准确的解决办法

    这篇文章主要介绍了PHP之浮点数计算比较以及取整数不准确的解决办法,代码超简单,需要的朋友可以参考下
    2015-07-07
  • 初识laravel5

    初识laravel5

    laravel5发布了,目前国内相关资料还比较少,只能先翻墙去官网先看看了,初步总结了一些变化,就想写下来。
    2015-03-03
  • Yii核心验证器api详解

    Yii核心验证器api详解

    这篇文章主要介绍了Yii核心验证器api,结合实例形式详细分析了Yii核心验证器(Core Validators)API相关方法与使用技巧,需要的朋友可以参考下
    2016-11-11
  • jQuery向下滚动即时加载内容实现的瀑布流效果

    jQuery向下滚动即时加载内容实现的瀑布流效果

    下拉滚动条或鼠标滚轮滚动到页面底部时, 动态即时加载新内容,通过本文给大家介绍jQuery向下滚动即时加载内容实现的瀑布流效果,感兴趣的朋友参考下
    2016-01-01
  • PHP MySql增删改查的简单实例

    PHP MySql增删改查的简单实例

    下面小编就为大家带来一篇PHP MySql增删改查的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • ThinkPHP3.2框架自定义配置和加载用法示例

    ThinkPHP3.2框架自定义配置和加载用法示例

    这篇文章主要介绍了ThinkPHP3.2框架自定义配置和加载用法,结合实例形式分析了thinkPHP3.2框架使用自定义配置保存预定义数组数据相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • php设计模式之单例模式用法经典示例分析

    php设计模式之单例模式用法经典示例分析

    这篇文章主要介绍了php设计模式之单例模式用法,结合具体实例形式分析了php单例模式的概念、原理、定义及使用方法,需要的朋友可以参考下
    2019-09-09
  • php无限遍历文件夹示例分享

    php无限遍历文件夹示例分享

    这篇文章主要介绍了php无限遍历文件夹示例,需要的朋友可以参考下
    2014-03-03
  • php防止网站被攻击的应急代码

    php防止网站被攻击的应急代码

    这篇文章为大家提供了php防止网站被攻击的应急代码,这是一个办法,绝对不是最好的解决方式,只是想提供给大家,大家一起探讨探讨。
    2015-10-10

最新评论