Home > Backend Development > PHP Tutorial > Implementation code and application examples of php shopping cart class

Implementation code and application examples of php shopping cart class

WBOY
Release: 2016-07-25 08:59:35
Original
985 people have browsed it
  1. class Shopcar
  2. {
  3. //Product list
  4. public $productList=array();
  5. /**
  6. *
  7. * @param unknown_type $product The product passed in
  8. * @return true There is no such product in the shopping cart
  9. */
  10. public function checkProduct($product)
  11. {
  12. for( $i=0;$iproductList);$i++ )
  13. {
  14. if($this->productList[$i]['name']==$product['name'] )
  15. return $i;
  16. }
  17. return -1;
  18. }
  19. //Add to cart
  20. public function add($product)
  21. {
  22. $i=$this->checkProduct($product);
  23. if( $i==-1)
  24. array_push($this->productList,$product);
  25. else
  26. $this->productList[$i]['num']+=$product['num'];
  27. }
  28. //Delete
  29. public function delete($product)
  30. {
  31. $i=$this->checkProduct($product);
  32. if($i!=-1)
  33. array_splice($this->productList, $i,1);
  34. }
  35. //Return all product information
  36. public function show()
  37. {
  38. return $this->productList;
  39. }
  40. }
Copy code

2. productList. html

  1. < ;html>
  2. php shopping cart-product list page-bbs .it-home.org
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template