购物车类
时间:2006-01-12 来源:wleige
sortCount = 0 ;
$this->totalCost = 0 ;
$this->myCart = array() ;
$this->mycheck = array() ;
}
//**********
//******************计算出购物车中的商品总数和总价格以及各家商店的产品总数量和总价格
//**********
function update()
{
$this->myCart = $_SESSION["myCart"] ;
if($this->myCart=="")
{
$this->sortCount = 0 ;
$this->totalCost = 0 ;
$this->check= array() ;
$this->myCart = array() ;
return false;
}
//开始计算商品的金额
foreach($this->myCart as $key=>$val)
{
foreach($val as $goodsid=>$var2)
{
$this->sortCount +=$this->myCart[$key][$goodsid]['num'];
$this->check[$key]['goodsnum']+=$this->myCart[$key][$goodsid]['num'];
$this->totalCost+=$this->myCart[$key][$goodsid]['num']*$this->myCart[$key][$goodsid]['price'];
$this->check[$key]['goodscost']+=$this->myCart[$key][$goodsid]['num']*$this->myCart[$key][$goodsid]['price'];
}
}
$_SESSION["myCart"] = $this->myCart ;
$_SESSION['check'] = $this->check ;
$_SESSION['sortcount']=$this->sortCount;
$_SESSION['totalcost']=$this->totalCost;
}
//************
//*******************增加商品
//*************
function addone($shopid,$goodsid,$name,$price)
{
$this->myCart=$_SESSION['myCart'];
$myCart=$this->myCart;
$myCart[$shopid][$goodsid]['goodsid']=$goodsid;
$myCart[$shopid][$goodsid]['name']=$name;
$myCart[$shopid][$goodsid]['price']=$price;
++$myCart[$shopid][$goodsid]['num'];
$_SESSION['myCart']=$myCart;
$this->update();
ob_end_flush();
}
//****************
//******************删除指定商品
//****************
function delone($shopid,$delid)
{
$checkcount=$_SESSION['check'];
$this->myCart=$_SESSION['myCart'];
if(!isset($this->myCart[$shopid][$delid]))
{
echo "alert('对不起,此商品已为空')";
}
else
{
foreach($this->myCart as $key=>$val)
{
foreach($val as $goodsid=>$var2)
{
if($this->myCart[$key][$goodsid]['goodsid']==$delid)
{
unset($this->myCart[$key][$goodsid]);
}
}
}
if(count($this->myCart[$shopid])=="0")
unset($this->myCart[$shopid]) ;
$_SESSION["myCart"] = $this->myCart ;
$this->update();
}
}
//*************
//*************************修改商品数量
//*************
function modify($shopid,$goodsid,$num)
{
$this->myCart=$_SESSION['myCart'];
$this->myCart[$shopid][$goodsid]['num']=$num;
$_SESSION['myCart']=$this->myCart;
$this->update();
}
//***************
//***********************打印出session中的内容,调试时使用
//***************
function print_cart()
{
echo "
";
print_r($_SESSION['myCart']);
echo"
";
echo $_SESSION['sortcount'];
echo "
";
echo $_SESSION['totalcost'];
echo"
";
print_r($_SESSION['check']);
}
//***************
//***********************删除SESSION中的内容
//***************
function delmycart()
{
unset($_SESSION['myCart']);
unset($_SESSION['check']);
unset($_SESSION['sortcount']);
unset($_SESSION['totalcost']);
}
}
//实例
$mycart=new cart_class;
$mycart->modify('ghx','0002','30'); //修改商品数量
//$mycart->delmycart(); //清除购物车
//$mycart->delone('ghx','0001'); //删除
//$mycart->delone('wleige','0003');
//$mycart->addone('wleige','0006','人参','45');//红高粮
//$mycart->addone('ghx','0002','非常可乐','3');
$mycart->print_cart();
?>
$this->totalCost = 0 ;
$this->myCart = array() ;
$this->mycheck = array() ;
}
//**********
//******************计算出购物车中的商品总数和总价格以及各家商店的产品总数量和总价格
//**********
function update()
{
$this->myCart = $_SESSION["myCart"] ;
if($this->myCart=="")
{
$this->sortCount = 0 ;
$this->totalCost = 0 ;
$this->check= array() ;
$this->myCart = array() ;
return false;
}
//开始计算商品的金额
foreach($this->myCart as $key=>$val)
{
foreach($val as $goodsid=>$var2)
{
$this->sortCount +=$this->myCart[$key][$goodsid]['num'];
$this->check[$key]['goodsnum']+=$this->myCart[$key][$goodsid]['num'];
$this->totalCost+=$this->myCart[$key][$goodsid]['num']*$this->myCart[$key][$goodsid]['price'];
$this->check[$key]['goodscost']+=$this->myCart[$key][$goodsid]['num']*$this->myCart[$key][$goodsid]['price'];
}
}
$_SESSION["myCart"] = $this->myCart ;
$_SESSION['check'] = $this->check ;
$_SESSION['sortcount']=$this->sortCount;
$_SESSION['totalcost']=$this->totalCost;
}
//************
//*******************增加商品
//*************
function addone($shopid,$goodsid,$name,$price)
{
$this->myCart=$_SESSION['myCart'];
$myCart=$this->myCart;
$myCart[$shopid][$goodsid]['goodsid']=$goodsid;
$myCart[$shopid][$goodsid]['name']=$name;
$myCart[$shopid][$goodsid]['price']=$price;
++$myCart[$shopid][$goodsid]['num'];
$_SESSION['myCart']=$myCart;
$this->update();
ob_end_flush();
}
//****************
//******************删除指定商品
//****************
function delone($shopid,$delid)
{
$checkcount=$_SESSION['check'];
$this->myCart=$_SESSION['myCart'];
if(!isset($this->myCart[$shopid][$delid]))
{
echo "alert('对不起,此商品已为空')";
}
else
{
foreach($this->myCart as $key=>$val)
{
foreach($val as $goodsid=>$var2)
{
if($this->myCart[$key][$goodsid]['goodsid']==$delid)
{
unset($this->myCart[$key][$goodsid]);
}
}
}
if(count($this->myCart[$shopid])=="0")
unset($this->myCart[$shopid]) ;
$_SESSION["myCart"] = $this->myCart ;
$this->update();
}
}
//*************
//*************************修改商品数量
//*************
function modify($shopid,$goodsid,$num)
{
$this->myCart=$_SESSION['myCart'];
$this->myCart[$shopid][$goodsid]['num']=$num;
$_SESSION['myCart']=$this->myCart;
$this->update();
}
//***************
//***********************打印出session中的内容,调试时使用
//***************
function print_cart()
{
echo "
";
print_r($_SESSION['myCart']);
echo"
";
echo $_SESSION['sortcount'];
echo "
";
echo $_SESSION['totalcost'];
echo"
";
print_r($_SESSION['check']);
}
//***************
//***********************删除SESSION中的内容
//***************
function delmycart()
{
unset($_SESSION['myCart']);
unset($_SESSION['check']);
unset($_SESSION['sortcount']);
unset($_SESSION['totalcost']);
}
}
//实例
$mycart=new cart_class;
$mycart->modify('ghx','0002','30'); //修改商品数量
//$mycart->delmycart(); //清除购物车
//$mycart->delone('ghx','0001'); //删除
//$mycart->delone('wleige','0003');
//$mycart->addone('wleige','0006','人参','45');//红高粮
//$mycart->addone('ghx','0002','非常可乐','3');
$mycart->print_cart();
?>
相关阅读 更多 +