php 购物车代码~呢?

2026年09月25日 21:48
有1个网友回答
网友(1):

class Shopcar 
{ 
//商品列表 
public $productList=array(); 
/** 
* 
* @param unknown_type $product 传进来的商品 
* @return true 购物车里面没有该商品 
*/ 
public function checkProduct($product) 
{ 
for($i=0;$iproductList);$i++ ) 
{ 
if($this->productList[$i]['name']==$product['name']) 
return $i; 
} 
return -1; 
} 
//添加到购物车 
public function add($product) 
{ 
$i=$this->checkProduct($product); 
if($i==-1) 
array_push($this->productList,$product); 
else 
$this->productList[$i]['num']+=$product['num']; 
} 
//删除 
public function delete($product) 
{ 
$i=$this->checkProduct($product); 
if($i!=-1) 
array_splice($this->productList,$i,1); 
} 
//返回所有的商品的信息 
public function show() 
{ 
return $this->productList; 
} 
} 
你可以去后盾人平台看看,里面的东西不错