Php shopping cart function implementation code (getting started example)

Source: Internet
Author: User
Php shopping cart function implementation code (getting started example)

  1. // Product category page
  2. Session_start (); // enable the session environment
  3. // Determine whether the user has clicked the "add to shopping cart" button
  4. If (isset ($ _ POST ['cart']) {
  5. // Value assignment in cyclic mode
  6. Foreach ($ _ POST ['cart'] as $ value ){
  7. // If this item exists in the shopping cart
  8. // Condition:
  9. // Click "Add to shopping cart". an array $ _ SESSION ['cart'] exists.
  10. // Determine the product ---- determine whether the product name has appeared 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 this item is not found in the shopping cart
  16. $ _ SESSION ['cart'] [$ value] = 1;
  17. }
  18. }
  19. }
  20. ?>
  21. Online shopping mall _ php shopping cart -bbs.it-home.org
  22. Online Store
  23. Daily necessities
  24. Household appliances
  25. Stationery
  26. View Shopping Cart
  27. Clear shopping cart

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

  1. // Display the items in the shopping cart (item name, item quantity)

  2. Session_start ();
  3. Echo "the items you have selected include:
    ";
  4. Echo "";
  5. Echo "back to homepage ";
  6. ?>

3. clear the cardel. php file to the shopping cart.

  1. Session_start (); // enable the session environment

  2. // Clear the shopping cart

  3. Unset ($ _ SESSION ['cart']);
  4. ?>

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.