Php shopping cart program

Source: Internet
Author: User
This is a simple php code developed and used by myself for the shopping cart function. a few files are used and no database is used to implement the shopping cart. if the user closes the browser, the items in the shopping cart will

This is a simple php code developed and used by myself for shopping cart functions. a few files are used and no database is used to implement shopping cart. if the user closes the browser, all the items in the shopping cart will be lost. if you need it, you can improve it. it is better to use database + session + cookie.

  1. Class Shopcar
  2. {
  3. // Product list
  4. Public $ productList = array ();
  5. /**
  6. *
  7. * @ Param unknown_type $ the product to be imported
  8. * @ Return true no such item exists in the shopping 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 shopping 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. }
  36. // Returns information about all products.
  37. Public function show ()
  38. {
  39. Return $ this-> productList;
  40. }
  41. }
  42. Html
  43.  
  44.  
  45.  
  46. Insert title here 
  47. Script
  48. Function buy (I)
  49. {
  50. Var num = $ (': input [name = num]') [I]. value;
  51. Var name = $ ('[name = name]') [I]. innerHTML;
  52. Var price = $ ('[name = price]') [I]. innerHTML;
  53. Alert (num + name + price );
  54. $. Ajax ({
  55. Type: 'post', // transfer method, get/post
  56. Url: 'Index. php', // address for sending data
  57. Cache: 'false ',
  58. Data: 'Num = '+ num + "& name =" + name + "& price =" + price,
  59. Success: function (data)
  60. {
  61. Alert (data );
  62. }
  63. })
  64.  
  65. }
  66. Script
  67.  
  68.  
  69.           'Show. php'> view shopping cart  
    Product No. Product Name Price Quantity Purchase
    0 Item 1
  70. 'Buy (0) '>'Blue '> purchase
    1 Product 2
  71. 'Buy (1) '> purchase
    2 Item 3
  72. 'Buy (2) '> purchase
    3 Product 4
  73. 'Buy (3) '> purchase
     
  74.  
  75.  
  76. Index. ph
  77. Require 'shopcar. class. php ';
  78. Session_start ();
  79. $ Name = $ _ POST ['name'];
  80. $ Num = $ _ POST ['num'];
  81. $ Price = $ _ POST ['price'];
  82. $ Product = array ('name' => $ name, 'num' => $ num, 'price' => $ price );
  83. Print_r ($ product );
  84. If (isset ($ _ SESSION ['shopcar '])
  85. $ Shopcar = unserialize ($ _ SESSION ['shopcar ']);
  86. Else
  87. $ Shopcar = new Shopcar ();
  88. $ Shopcar-> add ($ product );
  89. $ _ SESSION ['shopcar '] = serialize ($ shopcar );
  90. Show. php
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. Session_start ();
  98. $ Shopcar = unserialize ($ _ SESSION ['shopcar ']);
  99. Print_r ($ shopcar );
  100. $ ProductList = $ shopcar-> productList;
  101. Foreach ($ productList as $ product ){
  102. ?>
  103.  
  104. Require 'shopcar. class. php ';
  105.    
    Product No. Product Name Price Quantity
    1  
  106. '/>
     
  107.  
  108.  

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.