购物车代码

购物车代码<?php/***购物车**@copyrightCopyright(c)2006-2008coderhome.net*@author志凡<dzjzmj@163.com>*@packageModel*@versionv0.1*/classModelCartextendsModel{/***构造函…

<?php
/**
* 购物车
*
* @copyright Copyright (c) 2006 – 2008 coderhome.net
* @author 志凡 <dzjzmj@163.com>
* @package Model
* @version v0.1
*/

class ModelCart extends Model {

/**
* 构造函数
* @param Object $db 数据查询类
* @access public
* @return void
*/
function __construct($query=null) {
parent::__construct($query);
session_start();
}

function add($productInfo) {
$product = new ModelProduct();
$productItem = $product->getInfo($productInfo[‘id’]);
$info = array(
‘id’ => $productInfo[‘id’],
‘product_name’ => $productInfo[‘product_name’],
‘attribute’ => $productInfo[‘attribute’],
‘price’ => $productInfo[‘price’],
‘sn’ => $productItem[‘sn’],
‘photoUrl’ => CFG_URL . ‘data/product/’.$productItem[‘photo’],
‘number’ => 1,
);
$_SESSION[‘cartInfo’][$productInfo[‘id’].’_’.$productInfo[‘attribute’]] = $info;
}

function changeNumber($id,$number) {
$_SESSION[‘cartInfo’][$id][‘number’] = $number;
}
static function count() {
session_start();
return count($_SESSION[‘cartInfo’]);
}

function getAll() {
$items = $_SESSION[‘cartInfo’];
$result = array();
foreach ($items as $key => $item) {
$attrs = explode(‘||’,$item[‘attribute’]);
foreach ($attrs as $attr) {
$i = explode(‘=’,$attr);
$n = explode(‘,’,$i[1]);
$item[$i[0]] = $n[1];
$item[$i[0].’_id’] = $n[0];
}
$item[‘prices’] = $item[‘price’]*$item[‘number’];
$item[‘cart_id’] = urlencode($item[‘id’].’_’.$item[‘attribute’]);
$result[] = $item;
}
return $result;
}
function getCount() {
$items = $this->getAll();
$count = 0;
foreach ($items as $key => $item) {
$count += $item[‘prices’];
}
return $count;

}
function clear() {
$_SESSION[‘cartInfo’] = array();
}

function delete($id) {
unset($_SESSION[‘cartInfo’][$id]);
}

}
?>

http://www.corange.cn/archives/2008/10/2054.html

转载于:https://www.cnblogs.com/ajuanabc/archive/2009/03/17/2463283.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/38086.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注