PHP shopping cart implementation code_PHP tutorial
class
p
php
code
list
merchandise
copy
accomplish
shopping cart
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 ;$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:
< ;body>
Product number | Product name | Price | < ;td>QuantityPurchase | |
0 | | Buy | ||
1 | < ;label name='name' >Item 2 | | Buy | |
2 | Buy | |||
Purchase | ||||