用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2877|回复: 2
收起左侧

[HELP] 购物车类怎么判断商品已存在购物车中啊?

[复制链接]
发表于 2011-5-4 13:58:14 | 显示全部楼层 |阅读模式
我想通过商品ID判断商品是否已经存在购物车中!
放入购物车时,

PHP复制代码
 
$goods_id = $this->uri->segment(3); //获取商品ID;
$data = array(
        'id' => $goods_id,
        'qty' => 1,
        'price' => $price,
        'name' => $name,
        'options' => array('username'=>$session_name)
        );
        $this->cart->insert($data);
复制代码

然后要怎么判断要放入的商品是否已经存在购物车中啊?
是在控制器中判断吧,要用到
foreach ($this->cart->contents() as $itams){};
吗?
 楼主| 发表于 2011-5-4 15:44:21 | 显示全部楼层
本帖最后由 geomen 于 2011-5-4 15:47 编辑

没人回答!难道我问得太初级了,看来只能用笨办法了!
下面是自己想的办法!唉~
先自定义个函数:
PHP复制代码
function array_insert($myarray,$value,$position=0)
{
   $fore=($position==0)?array():array_splice($myarray,0,$position);
   $fore[]=$value;
   $ret=array_merge($fore,$myarray);
   return $ret;
}
复制代码

然后在控制器中:
PHP复制代码
$goods_id = $this->uri->segment(3);
                 foreach ($this->cart->contents() as $items):
                 static $id_array = array();
                $id_array = array_insert($id_array,$items['id'],1);
                 endforeach;
                if (in_array($goods_id,$id_array)){
                        echo '该商品已经在您的购物车中!';
                }else {
                $goods = $this->Mindex->get_one_goods($goods_id);  //从模块中获取商品数据。
                $price = $goods->s_price;
                $name = $goods->goods_name;
                $data = array(
                        'id' => $goods_id,
                        'qty' => 1,
                        'price' => $price,
                        'name' => $name,
                        'options' => array('username'=>$session_name)
                );
                $this->cart->insert($data);
                echo '放入购物车成功!您的购物车中有'.$this->cart->total_items().'件商品,总价:'.$this->cart->total().'¥。';
};
复制代码

点评

顶楼主一下下  发表于 2011-12-20 09:17
发表于 2011-12-20 17:42:59 | 显示全部楼层
CI的购物车类真的不咋地,╮(╯▽╰)╭

本版积分规则