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
983 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
  3. require 'Shopcar.class.php';
  4. session_start();
  5. $shopcar=unserialize($_SESSION['shopcar']);
  6. print_r($shopcar);
  7. $productList=$shopcar->productList;
  8. foreach ($productList as $product){
  9. ?>
  10. 商品编号商品名称价格数量
    1
  11. ' />
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template