Comparison | Shopping Cart
A project needs to be used for a shopping cart in the near future, so it is encapsulated in a class for later invocation, taking into account that it may be used frequently. You can simply change this class slightly can be used in their own program, the specific use see http://bigeagle.wotoo.com/article.asp?type=1.
Define this file constant
Define ("_cart_inc_", "exists");
/* Shopping Cart class * *
Class Tcart
{
var $SortCount; Number of types of goods
var $TotalCost; Total value of goods
var $Id; ID (array) for each category of goods
var $Name; Name of each type of commodity (array)
var $Price; Price of each type of commodity (array)
var $Discount; Discount for merchandise (array)
var $GoodPrice; Preferential prices for goods (array)
var $Count; Number of pieces per item (array)
var $MaxCount; Product Limits (Array)
Constructors
function Tcart ()
{
$this->sortcount=0;
Private, updating the corresponding data in the class based on the value of the session
function Update ()
{
Global $sId, $sName, $sPrice, $sCount, $sDiscount, $sMaxCount, $sGoodPrice;
Calculate the total number of items
$this->sortcount=count ($sId);
}
Private, calculating the value of each commodity and the total price of all goods according to the new data
function Calculate ()
{
For ($i =0 $i < $this->sortcount; $i + +)
{
/* Calculate the value of each item, if the discount is 0, then for the 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 are interface functions
Add a piece of merchandise
Judge whether there is already in the blue, if so, add count, otherwise add a new product
The first is to change the value of the session, and then call Update () and calculate () to update the member variable
function Add ($a _id, $a _name, $a _price, $a _discount,
$a _goodprice, $a _maxcount, $a _count)
{
Global $sId, $sName, $sCount, $sPrice, $sDiscount,
$sGoodPrice, $sMaxCount;
$k =count ($sId);
for ($i =0; $i < $k; $i + +)
{//Find out if you have already joined this product
if ($sId [$i]== $a _id)
{
$sCount [$i] + = $a _count;
Break
}
}
if ($i >= $k)
{//No, add a new product category
$sId [] = $a _id;
$sName [] = $a _name;
$sPrice [] = $a _price;
$sCount [] = $a _count;
$sGoodPrice [] = $a _goodprice;
$sDiscount [] = $a _discount;
$sMaxCount [] = $a _maxcount;
}
$this->update (); Update the member data for the class
$this->calculate ();
}
Remove a piece of merchandise
function Remove ($a _id)
{
Global $sId, $sName, $sCount, $sPrice, $sDiscount,
$sGoodPrice, $sMaxCount;
If a product is already in the blue, the parameter is the ID of the product
function Exists ($a _id)
{
For ($i =0 $i < $this->sortcount; $i + +)
{
if ($this->id[$i]== $a _id) return TRUE;
}
return FALSE;
}
The position 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 a piece of merchandise information, the main work function
Returns an associative 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;
}
Total number of items taken
function Cartcount ()
{
return $this->sortcount;
}
Take the total commodity value
function Gettotalcost ()
{
return $this->totalcost;
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.