PHP Shopping Cart class implementation code and application examples

Source: Internet
Author: User
  1. Class Shopcar
  2. {
  3. Product List
  4. Public $productList =array ();
  5. /**
  6. *
  7. * @param unknown_type $product in the product
  8. * @return True there is no item in the cart
  9. */
  10. Public Function checkproduct ($product)
  11. {
  12. for ($i =0; $i productlist); $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 information on all products
  36. Public Function Show ()
  37. {
  38. return $this->productlist;
  39. }
  40. }
Copy Code

2, productlist.html

  1. PHP Shopping Cart-Product List page-bbs.it-home.org
  2. View Shopping Cart
    Product number Product Name Price Number Buy
    0 Item 1 1
  3. Buy
    1 Item 2 2
  4. Buy
    2 Item 3 1
  5. Buy
    3 Item 4 1
  6. Buy
Copy Code

3, index.php

    1. Require ' Shopcar.class.php ';
    2. Session_Start ();
    3. $name =$_post[' name '];
    4. $num =$_post[' num '];
    5. $price =$_post[' price '];
    6. $product =array (' name ' = = $name, ' num ' = $num, ' price ' = ' $price ');
    7. Print_r ($product);
    8. if (Isset ($_session[' Shopcar '))
    9. $shopcar =unserialize ($_session[' Shopcar ');
    10. Else
    11. $shopcar =new Shopcar ();
    12. $shopcar->add ($product);
    13. $_session[' Shopcar ']=serialize ($shopcar);
    14. ?>
Copy Code

4, show.php

  1. Product Information Show Page _bbs.it-home.org
  2. Require ' Shopcar.class.php ';
  3. Session_Start ();
  4. $shopcar =unserialize ($_session[' Shopcar ');
  5. Print_r ($shopcar);
  6. $productList = $shopcar->productlist;
  7. foreach ($productList as $product) {
  8. ?>
  9. Product number Product Name Price Number
    1
  10. '/>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.