Home Backend Development PHP Tutorial 一个比较完善的购物车类_PHP

一个比较完善的购物车类_PHP

Jun 01, 2016 pm 12:25 PM
merchandise Compare shopping cart

前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用。你可以简单的把这个类稍微修改一下就可以用在自己的程序里了,具体使用请见。


/*****************************************************************************/
/* */
/* file type: 包含文件,建议后缀为.inc */
/* */
/* file name: cart.inc */
/* */
/* Description: 定义一个购车类 */
/* */
/* Func list : class cart */
/* */
/* author : bigeagle */
/* */
/* date : 2000/12/24 */
/* */
/* History: 2000/12/24 finished */
/* */
/*****************************************************************************/

//定义本文件常量
define("_CART_INC_" , "exists") ;

/*购物车类*/
class TCart
{

var $SortCount; //商品种类数
var $TotalCost; //商品总价值

var $Id; //每类商品的ID(数组)
var $Name; //每类商品的名称(数组)
var $Price; //每类商品的价格(数组)
var $Discount; //商品的折扣(数组)
var $GoodPrice ; //商品的优惠价格(数组)
var $Count; //每类商品的件数(数组)
var $MaxCount ; //商品限量(数组)

//******构造函数
function TCart()
{
$this->SortCount=0;

session_start(); //初始化一个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();
}

//********私有,根据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 ;

//计算商品总数
$this->SortCount=count($sId);

}

//********私有,根据新的数据计算每类商品的价值及全部商品的总价
function Calculate()
{
for($i=0;$iSortCount;$i++)
{
/*计算每件商品的价值,如果折扣是0 ,则为优惠价格*/
$GiftPrice = ($this->Discount[$i] == 0 ? $this->GoodPrice :
ceil($this->Price[$i] * $this->Discount[$i])/100 );
$this->TotalCost += $GiftPrice * $this->Count[$i] ;
}
}


//**************以下为接口函数

//*** 加一件商品
// 判断是否蓝中已有,如有,加count,否则加一个新商品
//首先都是改session的值,然后再调用update() and calculate()来更新成员变量
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{ //先找一下是否已经加入了这种商品
if($sId[$i]==$a_ID)
{
$sCount[$i] += $a_Count ;
break;
}
}
if($i >= $k)
{ //没有则加一个新商品种类
$sId[] = $a_ID;
$sName[] = $a_Name;
$sPrice[] = $a_Price;
$sCount[] = $a_Count;
$sGoodPrice[] = $a_GoodPrice ;
$sDiscount[] = $a_Discount ;
$sMaxCount[] = $a_MaxCount ;
}

$this->Update(); //更新一下类的成员数据
$this->Calculate();
}

//移去一件商品
function Remove($a_ID)
{
global $sId , $sName , $sCount , $sPrice , $sDiscount ,
$sGoodPrice , $sMaxCount ;

$k = count($sId);
for($i=0; $i {
if($sId[$i] == $a_ID)
{
$sCount[$i] = 0 ;
break;
}
}

$this->Update();
$this->Calculate();
}

//改变商品的个数
function ModifyCount($a_i,$a_Count)
{
global $sCount;

$sCount[$a_i] = $a_Count ;
$this->Update();
$this->Calculate();
}


/***************************
清空所有的商品
*****************************/
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 ;
}


//是否某件商品已在蓝内,参数为此商品的ID
function Exists($a_ID)
{
for($i=0; $iSortCount; $i++)
{
if($this->Id[$i]==$a_ID) return TRUE;
}
return FALSE;
}

//某件商品在蓝内的位置
function IndexOf($a_ID)
{
for($i=0; $iSortCount; $i++)
{
if($this->Id[$i]==$id) return $i;
}
return 0;
}

//取一件商品的信息,主要的工作函数
//返回一个关联数组,
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;
}

//取总的商品种类数
function CartCount()
{
return $this->SortCount;
}

//取总的商品价值
function GetTotalCost()
{
return $this->TotalCost;
}
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable nfc function on Xiaomi Mi 14 Pro? How to enable nfc function on Xiaomi Mi 14 Pro? Mar 19, 2024 pm 02:28 PM

Nowadays, the performance and functions of mobile phones are becoming more and more powerful. Almost all mobile phones are equipped with convenient NFC functions to facilitate users for mobile payment and identity authentication. However, some Xiaomi 14Pro users may not know how to enable the NFC function. Next, let me introduce it to you in detail. How to enable nfc function on Xiaomi 14Pro? Step 1: Open the settings menu of your phone. Step 2: Find and click the "Connect and Share" or "Wireless & Networks" option. Step 3: In the Connection & Sharing or Wireless & Networks menu, find and click "NFC & Payments". Step 4: Find and click "NFC Switch". Normally, the default is off. Step 5: On the NFC switch page, click the switch button to switch it to on.

How to use TikTok on Huawei Pocket2 remotely? How to use TikTok on Huawei Pocket2 remotely? Mar 18, 2024 pm 03:00 PM

Sliding the screen through the air is a feature of Huawei that is highly praised in the Huawei mate60 series. This feature uses the laser sensor on the phone and the 3D depth camera of the front camera to complete a series of functions that do not require The function of touching the screen is, for example, to use TikTok from a distance. But how should Huawei Pocket 2 use TikTok from a distance? How to take screenshots from the air with Huawei Pocket2? 1. Open the settings of Huawei Pocket2 2. Then select [Accessibility]. 3. Click to open [Smart Perception]. 4. Just turn on the [Air Swipe Screen], [Air Screenshot], and [Air Press] switches. 5. When using it, you need to stand 20~40CM away from the screen, open your palm, and wait until the palm icon appears on the screen.

How to set line spacing in WPS Word to make the document neater How to set line spacing in WPS Word to make the document neater Mar 20, 2024 pm 04:30 PM

WPS is our commonly used office software. When editing long articles, the fonts are often too small to be seen clearly, so the fonts and the entire document are adjusted. For example: adjusting the line spacing of the document will make the entire document very clear. I suggest that all friends learn this operation step. I will share it with you today. The specific operation steps are as follows, come and take a look! Open the WPS text file you want to adjust, find the paragraph setting toolbar in the [Start] menu, and you will see the small line spacing setting icon (shown as a red circle in the picture). 2. Click the small inverted triangle in the lower right corner of the line spacing setting, and the corresponding line spacing value will appear. You can choose 1 to 3 times the line spacing (as shown by the arrow in the figure). 3. Or right-click the paragraph and it will appear.

iPhone 16 Pro CAD drawings exposed, adding a second new button iPhone 16 Pro CAD drawings exposed, adding a second new button Mar 09, 2024 pm 09:07 PM

The CAD files of the iPhone 16 Pro have been exposed, and the design is consistent with previous rumors. Last fall, the iPhone 15 Pro added an Action button, and this fall, Apple appears to be planning to make minor adjustments to the size of the hardware. Adding a Capture button According to rumors, the iPhone 16 Pro may add a second new button, which will be the second consecutive year to add a new button after last year. It is rumored that the new Capture button will be set on the lower right side of the iPhone 16 Pro. This design is expected to make camera control more convenient and also allow the Action button to be used for other functions. This button will no longer be just an ordinary shutter button. Regarding the camera, from the current iP

The difference and comparative analysis between C language and PHP The difference and comparative analysis between C language and PHP Mar 20, 2024 am 08:54 AM

Differences and comparative analysis between C language and PHP C language and PHP are both common programming languages, but they have obvious differences in many aspects. This article will conduct a comparative analysis of C language and PHP and illustrate the differences between them through specific code examples. 1. Syntax and usage: C language: C language is a process-oriented programming language, mainly used for system-level programming and embedded development. The syntax of C language is relatively simple and low-level, can directly operate memory, and is efficient and flexible. C language emphasizes the programmer's completeness of the program

How to use Xiaomi Mi 14 Ultra AI smart image expansion? How to use Xiaomi Mi 14 Ultra AI smart image expansion? Mar 16, 2024 pm 12:37 PM

The progress of the times has made many people's incomes higher and higher, and the mobile phones they usually use will be changed frequently. The Xiaomi Mi 14 Ultra recently launched by Xiaomi must be familiar to users. It has very high performance configuration and can provide users with more In order to provide a comfortable and smooth experience, new mobile phones will inevitably encounter many functions that are not used. For example, how to use Xiaomi 14UltraAI smart image expansion? Come and take a look at the usage tutorial below! How to use Xiaomi 14UltraAI smart image expansion? First open Xiaomi 14Ultra, enter the photo album, select the picture you want to enlarge, and enter the photo album editing option. Click Crop Rotate, click Crop, and click Smart Expand in the selection that appears. Finally, choose the way to expand the image according to your own needs.

Comparison and analysis of advantages and disadvantages of PHP7.2 and 5 versions Comparison and analysis of advantages and disadvantages of PHP7.2 and 5 versions Feb 27, 2024 am 10:51 AM

Comparison and analysis of advantages and disadvantages of PHP7.2 and 5. PHP is an extremely popular server-side scripting language and is widely used in Web development. However, PHP is constantly being updated and improved in different versions to meet changing needs. Currently, PHP7.2 is the latest version, which has many noteworthy differences and improvements compared with the previous PHP5 version. In this article, we will compare PHP7.2 and PHP5 versions, analyze their advantages and disadvantages, and provide specific code examples. 1. Performance PH

TrendX Research Institute: Merlin Chain project analysis and ecological inventory TrendX Research Institute: Merlin Chain project analysis and ecological inventory Mar 24, 2024 am 09:01 AM

According to statistics on March 2, the total TVL of Bitcoin’s second-layer network MerlinChain has reached US$3 billion. Among them, Bitcoin ecological assets accounted for 90.83%, including BTC worth US$1.596 billion and BRC-20 assets worth US$404 million. Last month, MerlinChain’s total TVL reached US$1.97 billion within 14 days of launching staking activities, surpassing Blast, which was launched in November last year and is also the most recent and equally eye-catching. On February 26, the total value of NFTs in the MerlinChain ecosystem exceeded US$420 million, becoming the public chain project with the highest NFT market value besides Ethereum. Project Introduction MerlinChain is an OKX support

See all articles