A more complete shopping cart class

Source: Internet
Author: User
Tags define array count return
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.

?
/*****************************************************************************/
/*                                                                           */
/* File type: Include file, recommended suffix is. Inc *
/*                                                                           */
/* File Name:cart.inc * *
/*                                                                           */
/* Description: Define a car-buying category * *
/*                                                                           */
/* Func list:class cart * *
/*                                                                           */
* Author:bigeagle * *
/*                                                                           */
* DATE:2000/12/24 * *
/*                                                                           */
/* HISTORY:2000/12/24 Finished * *
/*                                                                           */
/*****************************************************************************/

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;

Session_Start (); Initializes 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, updating the corresponding data in the class based on the value of the session
function Update ()
{
Global $sId, $sName, $sPrice, $sCount, $sDiscount, $sMaxCount, $sGoodPrice;

if (!isset ($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 = $sCount;
$this->discount = $sDiscount;
$this->goodprice = $sGoodPrice;
$this->maxcount = $sMaxCount;

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;

$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 items
function Modifycount ($a _i, $a _count)
{
Global $sCount;

$sCount [$a _i] = $a _count;
$this->update ();
$this->calculate ();
}


/***************************
Clear all the merchandise.
*****************************/
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;
}


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;
}
}


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.