ShopCar.php
Copy code The code is as follows:
class Shopcar
{
//Product list
public $productList=array();
/**
*
* @param unknown_type $product The product passed in
* @return true There is no such product in the shopping cart
*/
public function checkProduct($product)
{
for($i=0 ;$iproductList);$i++ )
{
if($this->productList[$i]['name']==$product['name'] )
return $i;
}
return -1;
}
//Add to cart
public function add($product)
{
$ i=$this->checkProduct($product);
if($i==-1)
array_push($this->productList,$product);
else
$this ->productList[$i]['num']+=$product['num'];
}
//Delete
public function delete($product)
{
$i=$this->checkProduct($product);
if($i!=-1)
array_splice($this->productList,$i,1);
}
//Return all product information
public function show()
{
return $this->productList;
}
}
productList .html
Copy code The code is as follows:
Insert title here < ;body>