欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-08 20:05:12
本文实例讲述了基于ci(codeigniter)框架实现购物车功能的方法。分享给大家供大家参考,具体如下: 在商城项目中,购物车是非常重要的一环,此处留下源码,留作笔记!...

本文实例讲述了基于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程序设计有所帮助。