|
楼主 |
发表于 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().'¥。';
}; 复制代码 |
|