Practical php Shopping Cart program and practical php Shopping Cart program
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.
<? Php
// 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 <count ($ key); $ I ){
$ Items = $ cart-> modi ($ key [$ I], $ value [$ I]);
}
}
$ Items = $ cart-> getCart ();
// Print
Echo "<table border = 1> ";
Setlocale (LC_MONETARY, 'it _ it ');
Foreach ($ items as $ item ){
Echo "<tr> <form method =" post "action =" tmp. php "> ";
Echo "<td> ID :". $ item ['id']. "<input type = hidden name = key [] value = ". $ item ['id']. "> ";
Echo "<td> product:". $ item ['name'];
Echo "<td> unit price:". $ item ['price'];
Echo "<td> <input type = text name = value [] value =". $ item ['Count']. "> ";
$ Sum = $ item ['Count'] * $ item ['price'];
Echo "<td> total:". round ($ sum, 2 );
Echo "<td> <input type = button value = 'delete' onclick =" location = '? Action = remove & key = ". $ item ['id']." '"> ";
}
Echo "<input type = hidden name = action value = modi> ";
Echo "<tr> <td colspan = 7> <input type = submit/> ";
Echo "</td> </form> </tr> </table> ";
?>
<Hr>
<Form method = "post" action = "tmp. php">
ID: <input type = "text" name = "p []"/>
Product name: <input type = "text" name = "p []"/>
Unit Price: <input type = "text" name = "p []"/>
Quantity: <input type = "text" name = "p []"/>
<Input type = hidden name = action value = add>
<Input type = "submit"/>
</Form>
<?
/**
* Cart
*
* Shopping cart type
*
* @ Author doodoo <pWtitle@yahoo.com.cn>
* @ 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.