MORR
注册会员

UID 72404
精华
0
积分 66
帖子 48
金钱 66 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-4-26
状态 离线
|
发一个自己写的购物车类
<?
// 购物车类 by MORR 2006-10-24 23:48
// Update by MORR 2006-10-30 17:32
/*
修改 show() 函数可以完成如果出错时显示的信息.
使用说明:
require_once './include/cart.class.php';
$cart = new cart();
// 增加一个商品(商品编号,商品名称,商品单价,商品数量)
$cart->add(1, 'A饼干', 1.75, 1);
// 增加一个商品(商品编号,商品名称,单价,数量)(扩展字段名=值。。。可以无限扩展)
$cart->add(2, 'B饼干', 2.75, 2, array('field1' => '值1', 'field2' => '值2'));
// 修改一个商品数量(商品编号,新数量)
$cart->editcount(1, 2);
// 删除一个商品(商品编号)
$cart->delete(1);
// 清除购物车
$cart->clear();
// 返回购物车里所有商品数据,一个数组,一个商品占一行
// id 商品编号
// name 商品名称
// price 商品单价
// count 商品数量
// onetotal 一个商品总价格
$cartdata = $cart->getlist();
// 返回购物车所有商品总价格
$total = $cart->total();
// 返回购物车所有商品总数量
$totalcount = $cart->totalcount();
// 返回商品种类数量
$count = $cart->count();
*/
class cart {
// 购物车中商品数据
var $cartdata = array();
// 构造函数
function cart() {
if ($_COOKIE['cart_sid'])
session_id($_COOKIE['cart_sid']);
session_start();
if (!$_COOKIE['cart_sid'])
setcookie('cart_sid', session_id(), 3600*24 + time(), '/');
$this->readdata();
}
function readdata() {
if ($_SESSION['cartdata'] && is_array($_SESSION['cartdata']))
$this->cartdata = $_SESSION['cartdata'];
else
$this->cartdata = array();
}
// 增加一个商品
function add($id, $name, $price, $count, $ext = array()) {
$this->readdata();
if (empty($id) || empty($name) || empty($price) || empty($count) || $count <= 0)
$this->show('添加商品时数据不正确!');
// 商品已经存在,修改商品数量
if (!empty($this->cartdata[$id])) {
$this->editcount($id, $this->cartdata[$id]['count'] + $count);
} else {
$tmparray['id'] = $id;
$tmparray['name'] = $name;
$tmparray['price'] = $price;
$tmparray['count'] = $count;
$tmparray['onetotal'] = $price * $count;
// 扩展字段
if (is_array($ext) && !empty($ext))
foreach ($ext AS $key => $value)
$tmparray[$key] = $value;
$this->cartdata[$id] = $tmparray;
$this->save();
}
}
// 修改数量
function editcount($id, $count) {
$this->readdata();
if (empty($id) || empty($count) || $count <= 0 || empty($this->cartdata[$id]))
$this->show('修改商品数量时数据不正确!');
$this->cartdata[$id]['count'] = $count;
$this->cartdata[$id]['onetotal'] = $this->cartdata[$id]['price'] * $count;
$this->save();
}
// 修改数量
function delete($id) {
$this->readdata();
if (empty($id) || empty($this->cartdata[$id]))
$this->show('删除商品编号不正确或者已经不存在!');
unset($this->cartdata[$id]);
$this->save();
}
// 清除购物车
function clear() {
unset($this->cartdata);
$_SESSION['cartdata'] = '';
unset($_SESSION['cartdata']);
}
// 保存购物车数据
function save() {
if (!empty($this->cartdata))
$_SESSION['cartdata'] = $this->cartdata;
}
// 返回商品列表
function getlist() {
$this->readdata();
return $this->cartdata;
}
// 返回总价格
function total() {
$this->readdata();
$total = 0;
if (is_array($this->cartdata) && !empty($this->cartdata))
foreach ($this->cartdata AS $key => $value)
$total = $total + ($value['onetotal'] + 0);
return $total;
}
// 返回总数量
function totalcount() {
$this->readdata();
$total = 0;
if (is_array($this->cartdata) && !empty($this->cartdata))
foreach ($this->cartdata AS $key => $value)
$total = $total + ($value['count'] + 0);
return $total;
}
// 返回商品种类数量
function count() {
$this->readdata();
return count($this->cartdata);
}
// 出错显示
function show($str) {
$this->readdata();
?>
<script language="javascript">
alert('<?=addslashes($str)?>');
</script>
<?
exit;
}
}
?> 朋友要,上网找见没合适的,就自己写,自己还没实际用,请指导一下。
一个会话变量存储
|
|