Definition and usage example of Cart. class. PHP in php shopping cart class, car class Cart. class. php
This article describes the definition and usage of Cart. class. PHP In the php shopping Cart class. We will share this with you for your reference. The details are as follows:
Previous developers used JS technology to develop a set of front-end shopping cart (JS is used to delete and add anything), but the browser is not compatible with each other. Today there is finally a problem. A foreigner has bought products, because we use an unknown browser, chrome, opera... Therefore, I have an additional job to rewrite the shopping cart.
If you do not want to use JS any more, directly consider php.
Found a shopping cart class, easy to use.
Cart. class. php source code:
<? Php/*** Cart ** shopping Cart class ** @ 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; // Constructor of the Class var $ totalPrices; // total item amount/*** Cart Constructor, keep the shopping Cart in a stable initialization status ** @ 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 the item to the current shopping cart ** @ access public * @ param array $ item information (one-dimensional array: array (item ID, item name, item unit price, number of items) * @ 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 indicator $ key = current ($ item) of the array; if ($ key = "") return $ this-> cart; if ($ this-> _ isExists ($ key) {// does the product already exist? $ This-> cart [$ key] ['Count'] = end ($ item); return $ this-> cart ;} $ this-> cart [$ 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! =, From the current shopping cart, select the $ count item with the ID number of $ key ** @ access public * @ param string $ key item ID * @ return mixed to return the true or false or the item in the current shopping cart. array */function remove ($ key = "", $ count = "") {if ($ key = "") {$ this-> cart = array (); return true;} if (! Array_key_exists ($ key, $ this-> cart) return false; if ($ count = "") {// remove this type of 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 to $ value ** @ access public * @ param string $ key item ID * @ param int $ value * @ return array Returns an array of items in the current Shopping Cart. */function modi ($ key, $ value) {if (! $ This-> _ isExists ($ key) return $ this-> cart (); // if ($ value <= 0) is returned if ($ value <= 0) {// The value is too small. Delete All unsets ($ this-> cart [$ key]); return $ this-> cart ;} $ this-> cart [$ key] ['Count'] = $ value; return $ this-> cart ;} /*** return the array of items in the current shopping car** @ access public * @ return array returns the array of items in the current shopping cart; */function getCart () {return $ this-> cart;} // }}// {_ isExists ($ key) /*** determine whether there is a product with the ID of $ key in the current shopping cart ** @ access private * @ pa Ram 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, no product ** @ access public * @ return bool true or false; */function isEmpty () {return! Count ($ this-> cart);} // }}// {_ stat () /*** obtain partial statistical information ** @ 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 returned amount; */function totalPrices () {if ($ This-> _ stat () return $ this-> totalPrices; return 0;} // }}// {isEmpty () /*** get the total number of all items in the current shopping cart and ** @ access public * @ return int; */function totalCount () {if ($ this-> _ stat ()) return $ this-> totalCount; return 0 ;}// End Class Cart?>
Use this class:
<? Phpheader ("Content-type: text/html; charset = utf8"); // call the 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 ""; setlocale (LC_MONETARY, 'it _ it '); foreach ($ items as $ item) {echo ""; echo "<table border =" 1 "> <tbody> <tr> <form action = "\" Index. php \ "" method = "\" post \?? = ""> </Form> <td> ID :". $ item ['id']. "<input type =" hidden "value = ". $ item ['id']. "name =" key [] ">"; echo "</td> <td> product :". $ item ['name']; echo "</td> <td> unit price :". $ item ['price']; echo "</td> <input value = ". $ item ['Count']. "name =" value [] ">"; $ sum = $ item ['Count'] * $ item ['price']; echo "</td> <td> total :". round ($ sum, 2); echo "</td> <input onclick =" \ "location = '? Action = remove & key = ". $ item ['id']. "'\" "type =" button "value =" delete "> ";} echo "<input type =" hidden "value =" modi "name =" action "> "; echo "</td> </tr> <td colspan =" 7 "> <input type =" submit "value =" submit query content "> "; echo "</td> </tr> </tbody> </table>";?> <Hr> <form action = "tmp. php "method =" post "> ID: <input name =" p [] "> Item name: <input name =" p [] "> unit price: <input name = "p []"> quantity: <input name = "p []"> <input type = "hidden" value = "add" name = "action"> <input type = "submit" value = "submit a query content "> </form> </count>