fnet
高级会员

**村大队书记
UID 65788
精华
0
积分 717
帖子 784
金钱 717 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2005-8-12
状态 离线
|
[推荐阅读] 为什么我的IF语句没起作用呢?
购物车是最典型的Session应用实例
个人更推荐对象式的写法:
/**
* 产品模型类
*/
class Model_ProductBean {
var $id = 0; //id
var $price = 0; // 单价
var $name = null; //名城
var $count = 1; //数量
function setId( $id ) {
$this->id = $id;
}
function getId() {
return $this->id;
}
function setPrice( $price ) {
$this->price = $price;
}
function getPrice() {
return $this->price;
}
function setName( $name ) {
$this->name = $name;
}
function getName() {
return $this->name;
}
function setCount( $count ) {
$this->count = $count;
}
/**
* 获取商品数量
*
* @return int
*/
function getCount() {
return $this->count;
}
/**
* 获取产品总价格
*
* @return double
*/
function getTotalPrice() {
return $this->count * $this->price;
}
}
/**
* 对SESSION数据进行添加、删除、修改、统计
*
*/
class Helper_Cart {
var $catName;
function Helper_Cart( $catName = "" ) {
if ($catName == "")
$this->catName = "cat";
else
$this->catName = $catName;
}
/**
* 检测产品是否存在
*
* @param int $id
* @return boolean true/false
*/
function checkProductExsist( $id ) {
if ( 1 > count($_SESSION[$this->catName]) ) {
return false;
}
foreach ($_SESSION[$this->catName] as $key=>$val) {
if ($key == $id) {
return true;
}
}
return false;
}
/**
* 保存一个产品对象
*
* @param Object(Model_Product) $oProdcut
* @return int 产品个数
*/
function setProduct( $id , $oProdcut ) {
if ( !$this->checkProductExsist( $id ) ) {
$_SESSION[$this->catName][$id] = $oProdcut;
return 1;
}
$currentProduct = $_SESSION[$this->catName][$id];
$currentCount = $currentProduct->getCount()+1;
$currentProduct->setCount( $currentCount );
return $currentCount;
}
/**
* 根据产品id删除一个系列的产品
*
* @param int $id/-1 删除成功,返回删除id,删除不成功,返回-1
*/
function removeProduct( $id ) {
if ( $this->checkProductExsist($id) ) {
unset( $_SESSION[$this->catName][$id] );
return $id;
} else {
return -1;
}
}
/**
* 根据产品id删除一个产品
*
* @param int $id/-1 删除成功,返回删除的产品的id,没有这个商品,返回-1,商品正好为1个,则清楚这个id的商品 。
*/
function decreaseProduct($id) {
if ( !$this->checkProductExsist($id) ) {
return -1 ;
}
$currentProduct = $_SESSION[$this->catName][$id];
$currentCount = $currentProduct->getCount();
if ($currentCount == 1) {
unset($_SESSION[$this->catName][$id]);
return $id;
}
$currentProduct->setCount( $currentCount-1 );
return $id;
}
/**
* 清空整个购物车
*
*/
function cleanCat() {
if (isset($_SESSION[$this->catName])) {
unset( $_SESSION[$this->catName] );
}
return true;
}
/**
* 获取产品对象
*
* @param int $id
* @return Object(Model_Product)
*/
function getProduct( $id ) {
return $_SESSION[$this->catName][$id];
}
}
$oProduct1 = new Model_ProductBean();
$oProduct1->setId(1);
$oProduct1->setName("wazi");
$oProduct1->setPrice(123.00);
$oCat = new Helper_Cart();
$oCat->setProduct(1,$oProduct1);
$oProduct2 = new Model_ProductBean();
$oProduct2->setId(2);
$oProduct2->setName("toujing");
$oProduct2->setPrice(15.00);
$oCat->setProduct(2,$oProduct2);
$oProduct3 = new Model_ProductBean();
$oProduct3->setId(3);
$oProduct3->setName("xiezi");
$oProduct3->setPrice(23.00);
$oCat->setProduct(3,$oProduct3);
//$oCat->decreaseProduct(1);
$oProdcut = $oCat->getProduct(1);
$oProdcut2 = $oCat->getProduct(2);
$oProdcut3 = $oCat->getProduct(3);
echo $oProdcut->getTotalPrice()."\r\n";
echo $oProdcut2->getTotalPrice()."\r\n";
echo $oProdcut3->getTotalPrice()."\r\n";
print_r($_SESSION);
|  JAVA |
|