Practical php shopping cart program, Practical php shopping cart program. Practical php shopping cart program, Practical php shopping cart program Practical php Tutorial shopping cart program previously used a good feeling, but this feeling is also very good, so we will introduce the php shopping cart program and the php shopping cart program to be used.
Practical php Tutorial shopping cart program
I used to have a good experience before, but it also feels good to read it, so I would like to introduce it to a friend who needs it for reference.
// Call an instance
Require_once 'cart. class. php ';
Session_start ();
If (! Isset ($ _ SESSION ['cart']) {
$ _ SESSION ['cart'] = new cart;
}
$ Cart = & $ _ SESSION ['cart'];
If ($ _ SERVER ['request _ method'] = "POST") & ($ _ POST ['action'] = 'ADD ')){
$ P = $ _ POST ['P'];
$ Items = $ cart-> add ($ p );
}
If ($ _ GET ['action'] = 'delete') & ($ _ GET ['key']! = "")){
$ Items = $ cart-> remove ($ _ GET ['key']);
}
If ($ _ SERVER ['request _ method'] = "POST") & ($ _ POST ['action'] = 'Modi ')){
$ Key = $ _ POST ['key'];
$ Value = $ _ POST ['value'];
For ($ I = 0; $ I $ Items = $ cart-> modi ($ key [$ I], $ value [$ I]);
}
}
$ Items = $ cart-> getCart ();
// Print
Echo"
";Setlocale (LC_MONETARY, 'It _ it ');Foreach ($ items as $ item ){Echo"
";
?>
/**
* Cart
*
* Shopping cart type
*
* @ Author doodoo
* @ Package Cart
* @ Category Cart
* @ License PHP License
* @ Access public
* @ Version $ Revision: 1.10 $
*/
Class Cart {
Var $ cart;
Var $ totalCount; // total number of items
Var $ totalPrices; // total item amount
/**
* Cart Constructor
*
* Class constructor to ensure stable initialization of the shopping cart
*
* @ Static
* @ Access public
* @ Return void: no return value
* @ Param void no parameter
*/
Function Cart (){
$ This-> totalCount = 0;
$ This-> totalPrice = 0;
$ This-> cart = array ();
}
//}}}
// {Add ($ item)
/**
* Add items to the current shopping cart
*
* @ Access public
* @ Param array $ item product information (one-dimensional array: array (product ID, product name, product unit price, product quantity ))
* @ Return array returns the array of items in the current shopping cart.
*/
Function add ($ item ){
If (! Is_array ($ item) | is_null ($ item) return $ this-> cart;
If (! Is_numeric (end ($ item) | (! Is_numeric (prev ($ item )))){
Echo "the price and quantity must be numbers ";
Return $ this-> cart;
}
Reset ($ item); // This sentence is required, because the above judgment has moved the index of the array
$ Key = current ($ item );
If ($ key = "") return $ this-> cart;
If ($ this-> _ isExists ($ key) {// is the product already exists?
$ This-> cart [$ key] ['count'] = end ($ item );
Return $ this-> cart;
}
$ This-> ca (www.111cn.net) rt [$ key] ['id'] = $ key;
$ This-> cart [$ key] ['name'] = next ($ item );
$ This-> cart [$ key] ['price'] = next ($ item );
$ This-> cart [$ key] ['count'] = next ($ item );
Return $ this-> cart;
}
//}}}
// {Add ($ item)
/**
* Retrieve some or all items from the current shopping cart
* When $ key = "", clear the current shopping cart
* When $ key! = "" & $ Count = "", select all items whose ID number is $ key from the current shopping cart.
* When $ key! = "" & $ Count! = "", Select $ count items with the ID number of $ key from the current shopping cart.
*
* @ Access public
* @ Param string $ key product ID
* @ Return mixed returns an array of true and false items or items in the current shopping cart
*/
Function remove ($ key = "", $ count = ""){
If ($ key = ""){
$ This-> cart = array ();
Return true;
}
If (! Array_key_exists ($ key, $ this-> cart) return false;
If ($ count = "") {// remove this item
Unset ($ this-> cart [$ key]);
} Else {// remove $ count items
$ This-> cart [$ key] ['count']-= $ count;
If ($ this-> cart [$ key] ['count'] <= 0) unset ($ this-> cart [$ key]);
}
Return $ this-> cart;
}
//}}}
// {Modi ($ key, $ value)
/**
* Change the number of items in the shopping cart with the product ID as $ key to $ value.
*
* @ Access public
* @ Param string $ key product ID
* @ Param int $ value number of items
* @ Return array returns the array of items in the current shopping cart;
*/
Function modi ($ key, $ value ){
If (! $ This-> _ isExists ($ key) return $ this-> cart (); // this item does not exist and is returned directly
If ($ value <= 0) {// value is too small, delete all
Unset ($ this-> cart [$ key]);
Return $ this-> cart;
}
$ This-> cart [$ key] ['count'] = $ value;
Return $ this-> cart;
}
/**
* Returns an array of items in the current shopping cart.
*
* @ Access public
* @ Return array returns the array of items in the current shopping cart;
*/
Function getCart (){
Return $ this-> cart;
}
//}}}
// {_ IsExists ($ key)
/**
* Check whether there is a product with the ID of $ key in the current shopping cart.
*
* @ Access private
* @ Param string $ key product ID
* @ Return bool true or false;
*/
Function _ isExists ($ key)
{
If (isset ($ this-> cart [$ key]) &! Empty ($ this-> cart [$ key]) & array_key_exists ($ key, $ this-> cart ))
Return true;
Return false;
}
//}}}
// {IsEmpty ()
/**
* Determines whether the current shopping cart is empty, that is, there is no product
*
* @ Access public
* @ Return bool true or false;
*/
Function isEmpty (){
Return! Count ($ this-> cart );
}
//}}}
// {_ Stat ()
/**
* Obtain partial statistics
*
* @ Access private
* @ Return bool true or false;
*/
Function _ stat (){
If ($ this-> isEmpty () return false;
Foreach ($ this-> cart as $ item ){
$ This-> totalCount = @ end ($ item );
$ This-> totalPrices = @ prev ($ item );
}
Return true;
}
//}}}
// {TotalPrices ()
/**
* Get the total amount of all items in the current shopping cart
*
* @ Access public
* @ Return float: The Returned amount;
*/
Function totalPrices (){
If ($ this-> _ stat ())
Return $ this-> totalPrices;
Return 0;
}
//}}}
// {IsEmpty ()
/**
* Obtain the total quantity and total number of all items in the current shopping cart.
*
* @ Access public
* @ Return int;
*/
Function totalCount (){
If ($ this-> _ stat ())
Return $ this-> totalCount;
Return 0;
}
} // End Class Cart
?>
From: http://www.111cn.net/phper/php-gj/39684.htm
How to use PHP to compile a shopping cart program?
MySQL is not a difficult task. it is nothing more than statements such as query and insertion. The key is the temporary increase, decrease, and maintenance of the selected items in the shopping cart, you need to serialize the product information and use the front-end COOKIE for temporary storage. if you want to achieve better real-time interactive operations such as page refreshing, you need to add AJAX + JSON technology ~~
Score given by the landlord ~~~ I can only give you a reference ~~
How does php call the shopping cart program?
This is a php class. it can only be called in php, and it is not a program, but it is only a tenth of the process.
The Practical php Tutorial of the shopping cart program of the workshop used to have a good feeling before, but it also feels good after reading this, so it is introduced to the needs...