<?php class cart{Public Function Cart () {if (!isset ($_session[' cart ')) {$_session[' cart '] = array (); }/* Add commodity param int $id Commodity primary key string $name commodity name float $price commodity price int $num Shopping quantity */Public function ad Ditem ($id, $name, $price, $num, $img) {//If the commodity already exists, add the quantity if (Isset ($_session[' cart '] [$id])) {$this->incnum ($id, $nu
m);
Return
} $item = Array ();
$item [' id '] = $id;
$item [' name '] = $name;
$item [' price '] = $price;
$item [' num '] = $num;
$item [' img '] = $img;
$_session[' cart ' [$id] = $item; /* Modify the number of items in the shopping cart int $id primary KEY int $num The number of changes to a product, that is, to change the quantity of an item directly to $num/Public function Modnum ($id, $num =1) {if!
Isset ($_session[' cart ' [$id])) {return false;
$_session[' cart ' [$id] [' num '] = $num; }/* Product quantity +1/Public function Incnum ($id, $num =1) {if (Isset ($_session[' cart '] [$id])) {$_session[' cart '] [$id] [
' num '] + = $num; }/* Commodity quantity-1/Public Function decnum ($id, $num =1) {if isset ($_session[' cart '] [$iD]) {$_session[' cart '] [$id] [' num ']-= $num;
If the quantity is reduced to 0, delete the product if ($_session[' cart '] [$id] [' num '] <1) {$this->delitem ($id);
}/* Delete commodity/Public Function Delitem ($id) {unset ($_session[' cart '] [$id]);
/* Get a single product/Public function GetItem ($id) {return $_session[' cart '] [$id];
} * * Query the type of merchandise in the cart/Public Function getcnt () {return count ($_session[' cart '));
} * * Query the number of items in the cart/Public Function getnum () {if ($this->getcnt () = 0) {//0, number is 0 return 0;
} $sum = 0;
$data = $_session[' cart '];
foreach ($data as $item) {$sum + = $item [' num '];
return $sum;
/* * The total amount of merchandise in the cart/Public Function GetPrice () {//The quantity is 0, the price is 0 if ($this->getcnt () = = 0) {return 0;
} $price = 0.00;
foreach ($this->items as $item) {$price + = $item [' num '] * $item [' price '];
Return sprintf ("%01.2f", $price);
/* Empty cart/Public Function clear () {$_session[' cart '] = array (); }
}