本文实例讲述了基于CI(CodeIgniter)框架实现购物车功能的方法。分享给大家供大家参考,具体如下:
在商城项目中,购物车是非常重要的一环,此处留下源码,留作笔记!!!
话不多说,往下看:
1. 源代码
<"商品库存不足"; $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; } } "font-size: medium">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程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 卓依娜姆《美人谋 网剧影视原声带》[320K/MP3][48.29MB]
- 张星特.2024-流绪呢喃【中视鸣达】【FLAC分轨】
- 邓紫棋.2010-MYSECRET【蜂鸟音乐】【WAV+CUE】
- 江惠仪.2011-感謝您【禾广娱乐】【FLAC分轨】
- 韩红2005-精选[首版][WAV+CUE]
- 张敬轩.2004-A.M/P.M【几何文化】【WAV+CUE】
- 范文芳1999-真心话电影原声带[HYPE][WAV+CUE]
- 袁培华.1990-我写不出一首情歌给我最爱的你【可登】【WAV+CUE】
- 群星.2007-歌林10年流行岁月12CD【歌林】【WAV+CUE】
- 软硬天师.2006-LONG.TIME.NO.SEE【金牌大风】【WAV+CUE】
- 卓依娜姆《美人谋 网剧影视原声带》[FLAC/分轨][162.27MB]
- 国风大师纯音系列《蒋倩 :古筝演奏家》1CD[MP3][160.7MB]
- 国风大师纯音系列《王中山古筝》1CD[MP3][519MB]
- 徐玮1986-走自己的路[台湾复刻版][WAV+CUE]
- 黄思婷2004-情缘[豪记唱片][WAV+CUE]