Not long ago, you need to use a shopping cart, considering that it is often used, so it is packaged into a class so that it will be called later. You can simply modify this class, you can use it in your own program, please see it for details.
/ ************************************************** *************************** /
/ * * /
/ * file type: contains files, suggests the suffix .inc * /
/ * * /
/ * file name: cart.inc * /
/ * * /
/ * DESCRIPTION: Define a car * /
/ * * /
/ * Func List: Class Cart * /
/ * * /
/ * Author: BiGeagle * /
/ * * /
/ * Date: 2000/12/24 * /
/ * * /
/ * History: 2000/12/24 finished * /
/ * * /
/ ************************************************** *************************** /
// Define the constant of this file
Define ("_ cart_inc_", "exists");
/ * Shopping car * /
Class TCART
{
Var $ sortcount; // Product species
Var $ TOTALCOST; // Total value
Var $ ID; / / / / ID (array) for each type of product
Var $ name; // Name of each type of product (array)
Var $ price; // Price of each type of product (array)
Var $ discount; // Product discount (array)
Var $ goodprice; // Product offer price (array)
Var $ count; // Number of pieces of each type of item (array)
Var $ maxcount; // Product Limited (Array)
//******Constructor
Function TCART ()
{
$ this-> sortcount = 0;
session_start (); // Initialize a session
Session_register ('sID');
Session_register ('sname');
Session_register ('sprice');
Session_register ('sdiscount');
Session_register ('sgoodprice');
Session_register ('scount');
Session_register ('smaxcount');
$ this-> Update ();
$ this-> Calculate ();
}
// ******** Private, according to the SESSION value update the corresponding data
Function update ()
{
GLOBAL $ SID, $ SAME, $ SPRICE, $ SCOUNT, $ SDISCOUNT, $ SMAXCOUNT, $ SGOODPRICE
IF ($ SID) OR! Isset ($ SNAME) OR! Isset ($ SPRICE)
OR! Isset ($ SDISCOUNT) OR! Isset ($ SMAXCOUNT)
Or! Isset ($ SGOODPRICE) OR! ISSET ($ Scount)) Return;
$ this-> id = $ sID;
$ this-> name = $ SNAME;
$ this-> price = $ sprice; $ this-> count = $ sn;
$ this-> discount = $ sdiscount;
$ this-> goodprice = $ sgoodprice;
$ this-> maxcount = $ smaxcount;
// Total number of copies of goods
$ this-> sortcount = count ($ sID);
}
// ******** Private, calculate the value of each type of product and the total price of all products, according to new data
Function Calculate ()
{
For ($ I = 0; $ i <$ this-> sortcount; $ i )
{
/ * Calculate the value of each item, if the discount is 0, it is preferential price * /
$ Giftprice = ($ this-> discount [$ i] == 0? $ This-> goodprice:
CEIL ($ this-> price [$ I] * $ this-> discount [$ I]) / 100);
$ this-> totalcost = $ giftprice * $ this-> count [$ I];
}
}
// ************** The following is an interface function
// *** Add a item
/ / Judgment whether there is already blue, if there is, add count, otherwise add a new product
// First of all, the value of the session is changed, then call Update () and Calculate () to update member variables.
Function add ($ A_ID, $ A_NAME, $ A_PRICE, $ A_DISCOUNT,
$ A_GOODPRICE, $ A_MAXCOUNT, $ A_COUNT?
{
GLOBAL $ SID, $ SAME, $ SCOUNT, $ SPRICE, $ SDISCOUNT,
$ SGOODPRICE, $ SMAXCOUNT;
$ k = count ($ sID);
For ($ I = 0; $ i <$ k; $ i )
{// first find this product has been added
IF ($ SID [$ I] == $ A_ID)
{
$ SCOUNT [$ I] = $ A_COUNT;
Break;
}
}
IF ($ I> = $ K)
{// No added a new product species
$ SID [] = $ A_ID;
$ SNAME [] = $ A_NAME;
$ SPRICE [] = $ A_PRICE;
$ scount [] = $ A_COUNT;
$ SGOODPRICE [] = $ A_GOODPRICE;
$ sdiscount [] = $ a_discount;
$ SMAXCOUNT [] = $ A_MAXCOUNT;
}
$ this-> update (); // Update member data
$ this-> Calculate ();
}
// Remove one item
Function Remove ($ A_ID)
{
GLOBAL $ SID, $ SAME, $ SCOUNT, $ SPRICE, $ SDISCOUNT,
$ SGOODPRICE, $ SMAXCOUNT;
$ k = count ($ sID);
For ($ I = 0; $ i <$ k; $ i )
{
IF ($ SID [$ I] == $ A_ID)
{
$ SCOUNT [$ I] = 0;
Break;
}
}
$ this-> Update ();
$ this-> Calculate ();
}
/ / Change the number of goods
Function ModifyCount ($ A_I, $ A_COUNT) {
Global $ Scount;
$ SCOUNT [$ A_I] = $ A_COUNT;
$ this-> Update ();
$ this-> Calculate ();
}
/ ***************************
Empty all products
**************************** /
Function RemoveAll ()
{
Session_unregister ('sID');
Session_unregister ('sname');
Session_unregister ('sprice');
Session_unregister ('sdiscount');
Session_unregister ('sgoodprice');
Session_unregister ('Scount');
Session_unregister ('smaxcount');
$ this-> sortcount = 0;
$ this-> Totalcost = 0;
}
/ / Whether a piece of item has been in the blue, the parameter is the ID of this product
Function exists ($ A_ID)
{
For ($ I = 0; $ i <$ this-> sortcount; $ i )
{
IF ($ this-> ID [$ I] == $ A_ID) Return True;
}
Return False;
}
// Location of a product in the blue
Function IndexOf ($ A_ID)
{
For ($ I = 0; $ i <$ this-> sortcount; $ i )
{
IF ($ this-> ID [$ I] == $ ID) Return $ I;
}
Return 0;
}
// Take information of a commodity, the main work function
// Return an associated array,
Function Item ($ I)
{
$ Result [ID] = $ this-> ID [$ I];
$ Result [name] = $ this-> name [$ I];
$ Result [price] = $ this-> price [$ I];
$ Result [count] = $ this-> count [$ I];
$ Result [discount] = $ this-> discount [$ I];
$ Result [Goodprice] = $ this-> goodprice [$ i];
$ Result [Maxcount] = $ this-> maxcount [i];
Return $ Result;
}
// Take a total number of goods
Function Cartcount ()
{
Return $ this-> sortcount;
}
// Take a total value of goods
Function gettotalcost ()
{
Return $ this-> Totalcost
}
}