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.
-
- Class Shopcar
- {
- // Product list
- Public $ productList = array ();
- /**
- *
- * @ Param unknown_type $ the product to be imported
- * @ Return true no such item exists in the shopping cart
- */
- Public function checkProduct ($ product)
- {
- For ($ I = 0; $ I ProductList); $ I ++)
- {
- If ($ this-> productList [$ I] ['name'] = $ product ['name'])
- Return $ I;
- }
- Return-1;
- }
- // Add to shopping cart
- Public function add ($ product)
- {
- $ I = $ this-> checkProduct ($ product );
- If ($ I =-1)
- Array_push ($ this-> productList, $ product );
- Else
- $ This-> productList [$ I] ['num'] + = $ product ['num'];
- }
- // Delete
- Public function delete ($ product)
- {
- $ I = $ this-> checkProduct ($ product );
- If ($ I! =-1)
- Array_splice ($ this-> productList, $ I, 1 );
-
- }
- // Returns information about all products.
- Public function show ()
- {
- Return $ this-> productList;
- }
- }
- Html
-
-
-
-
- Insert title here
- Script
-
- Function buy (I)
- {
- Var num = $ (': input [name = num]') [I]. value;
- Var name = $ ('[name = name]') [I]. innerHTML;
- Var price = $ ('[name = price]') [I]. innerHTML;
- Alert (num + name + price );
- $. Ajax ({
- Type: 'post', // transfer method, get/post
- Url: 'Index. php', // address for sending data
- Cache: 'false ',
- Data: 'Num = '+ num + "& name =" + name + "& price =" + price,
- Success: function (data)
- {
- Alert (data );
- }
- })
-
- }
- Script
-
-
Product No. |
Product Name |
Price |
Quantity |
Purchase |
0 |
Item 1 |
1 |
|
'Buy (0) '>'Blue '> purchase |
1 |
Product 2 |
2 |
|
'Buy (1) '> purchase |
2 |
Item 3 |
1 |
|
'Buy (2) '> purchase |
3 |
Product 4 |
1 |
|
'Buy (3) '> purchase |
'Show. php'> view shopping cart
-
-
- Index. ph
-
- Require 'shopcar. class. php ';
- Session_start ();
- $ Name = $ _ POST ['name'];
- $ Num = $ _ POST ['num'];
- $ Price = $ _ POST ['price'];
- $ Product = array ('name' => $ name, 'num' => $ num, 'price' => $ price );
- Print_r ($ product );
- If (isset ($ _ SESSION ['shopcar '])
- $ Shopcar = unserialize ($ _ SESSION ['shopcar ']);
- Else
- $ Shopcar = new Shopcar ();
- $ Shopcar-> add ($ product );
- $ _ SESSION ['shopcar '] = serialize ($ shopcar );
- Show. php
-
-
-
-
-
-
-
-
- Session_start ();
- $ Shopcar = unserialize ($ _ SESSION ['shopcar ']);
- Print_r ($ shopcar );
- $ ProductList = $ shopcar-> productList;
- Foreach ($ productList as $ product ){
- ?>
-
- Require 'shopcar. class. php ';
Product No. |
Product Name |
Price |
Quantity |
1 |
|
|
'/> |
-
-