<?
// shopping basket class
// Writer : Minseok Choi(eraser95@korea.com)
// Date : 2001-06-11
//
// save items to buy in sessions
class item {
var $code; // code
var $name; // name
var $quantity; // quantity
var $price; // price per item
function item($code, $name, $quantity, $price) {
$this->code = $code;
$this->name = $name;
$this->quantity = $quantity;
$this->price = $price;
}
}
/**
* shopping basket class
*/
class basket {
/**
* constructor
*/
function basket() {
$this->sessionStart();
}
/**
* start session
*/
function sessionStart() {
global $session_basket;
session_register(\\\"session_basket\\\");
$session_basket = Array();
}
/**
* destory session
*/
function sessionEnd() {
session_unset();
session_destroy();
}
function basketSize() {
global $session_basket;
// make session if not found
if ($session_basket == \\\"\\\") {
$this->sessionStart();
}
if (! is_array($session_basket)) {
return 0;
}
/*
reset($session_basket);
while(list($k, $v) = each($session_basket)) {
$i++;
echo $k . \\\"==>\\\" . $v . \\\"<br>\\\";
while(list($key, $value) = each($session_basket[$k])) {
echo \\\" \\\" . $key . \\\"==>\\\" . $value . \\\"<br>\\\";
}
}
*/
return $i;
}
/**
* register item in session
* if same code exist in session, modify it.
*/
function registerItem($code, $name, $quantity, $price) {
global $session_basket;
// make session if not found
if ($session_basket == \\\"\\\") {
$this->sessionStart();
}
// same code exist??
if (! $this->editItem($code, $name, $quantity, $price)) {
$item = new item($code, $name, $quantity, $price);
$session_basket[] = $item;
}
}
/**
* same code exist??
* if exist, modify it.
*/
function editItem($code, $name, $quantity, $price) {
global $session_basket;
// make session if not found
if ($session_basket == \\\"\\\") {
$this->sessionStart();
return false;
}
reset($session_basket);
while(list($k, $v) = each($session_basket)) {
if ($session_basket[$k]->code == $code) {
// Found same code
$session_basket[$k]->name == $name;
$session_basket[$k]->quantity = $quantity;
$session_basket[$k]->price = $price;
return true;
}
}
return false;
}
/**
* delete item
*/
function deleteItem($code, $name) {
global $session_basket;
// make session if not found
if ($session_basket == \\\"\\\") {
$this->sessionStart();
}
reset($session_basket);
while(list($k, $v) = each($session_basket)) {
if ($session_basket[$k]->code == $code) {
unset($session_basket[$k]);
return true;
}
}
}
}
// sample
$basket = new basket();
$basket->registerItem(\\\"asf000000\\\", \\\"implant\\\", \\\"1\\\", \\\"1000000\\\");
$basket->registerItem(\\\"abcd000001\\\", \\\"avana implant kits\\\", \\\"10\\\", \\\"15000\\\");
$basket->registerItem(\\\"asf000000\\\", \\\"implant\\\", \\\"5\\\", \\\"0\\\");
$basket->deleteItem(\\\"asf000000\\\", \\\"implant\\\");
$basket->deleteItem(\\\"abcd000001\\\", \\\"avana implant kits\\\");
$basket->registerItem(\\\"asf000000\\\", \\\"implant\\\", \\\"1\\\", \\\"1000000\\\");
$basket->registerItem(\\\"abcd000001\\\", \\\"avana implant kits\\\", \\\"10\\\", \\\"15000\\\");
$basket->deleteItem(\\\"asf000000\\\", \\\"implant\\\");
$basket->deleteItem(\\\"abcd000001\\\", \\\"avana implant kits\\\");
$basket->registerItem(\\\"asf000000\\\", \\\"implant\\\", \\\"1\\\", \\\"1000000\\\");
$basket->registerItem(\\\"abcd000001\\\", \\\"avana implant kits\\\", \\\"10\\\", \\\"15000\\\");
// session
reset($session_basket);
while(list($k, $v) = each($session_basket)) {
$i++;
echo $k . \\\"==>\\\" . $v . \\\"<br>\\\";
while(list($key, $value) = each($session_basket[$k])) {
echo \\\" \\\" . $key . \\\"==>\\\" . $value . \\\"<br>\\\";
}
}
// logout
$basket->sessionEnd();
?>
© Lynne Grewe