PHP Shopping Cart feature Implementation code (introductory example)

Source: Internet
Author: User
  1. Product Category page
  2. Session_Start ();//Open Session Environment
  3. Determine if the user has clicked the "Add to Cart" button
  4. if (isset ($_post[' cart ')) {
  5. Cyclic mode Assignment
  6. foreach ($_post[' cart ') as $value) {
  7. If there is a product in the shopping cart
  8. Conditions:
  9. Clicked "Put into cart"---existence array $_session[' cart '];
  10. Determine which commodity----determine whether the name of the product appears in the array
  11. Array_key_exists (subscript, array);
  12. if (isset ($_session[' cart ')) && array_key_exists ($value, $_session[' cart ')) {
  13. $_session[' cart ' [$value]++;
  14. }else{
  15. If the item is not in the shopping cart
  16. $_session[' cart ' [$value]=1;
  17. }
  18. }
  19. }
  20. ?>
  21. Online mall _php Shopping Cart-bbs.it-home.org
  22. Online Shop

  23. Daily Necessities
  24. Household appliances
  25. Cultural goods
  26. View Shopping Cart
  27. Empty shopping Cart
Copy Code

2. carlist.php display the items in the shopping cart

    1. Display the items in the cart (name of product, number of items)

    2. Session_Start ();
    3. echo "The items you have selected include:
      ";
    4. echo "";
    5. echo "Back Home";
    6. ?>
Copy Code

3, file cardel.php empty shopping Cart

    1. Session_Start ();//Open Session Environment

    2. Empty shopping Cart

    3. unset ($_session[' cart ');
    4. ?>
Copy Code

The above code realizes a shopping cart common function, displays the commodity classification and the information, the shopping cart product processing and so on. We hope to help you.

  • 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.