鬼泣 发表于 2015-8-28 07:39:02

PHP购物车类

  
  <?php
include("session.ini");
/**********************************/
/*****PHP购物车类*******************/
/*****作者:王亚君 李瑜欣2009-7-6v1.0.1****/
/**********************************/

///*购物车类,本购物车类的设计原理是 购物车数据将只记录商品id和数量我们把这两个值做为键值对生成一个购物车数组变量并放到Session中,其它价格,打折与否等信息会在如查看购物车等方法的调用时从数据库中查找并计算,这样做的好处时价格和打折与否等信息可能更新而不准确,如果提前把这些信息提取到购物车数组变量中,在客户整个购物过程中数据有可能发生了改变*///
//include("session.ini");//如果你已经在头部php文件包含了session.ini这里就不用再包含
class Cart
{
/// <summary>
      /// 把指定商品加入购物车
      /// </summary>
      /// <param name="$id">要加入购物车的商品id</param>
public functionAddToCart($id)
{
   //如果指定的商品ID在购物车数组变量中没有那说明是用户第一次买这个商品      
   
   if(empty($_SESSION['myCart'][$id]))
   {
    $_SESSION['myCart'][$id] = 1;
   }
   //如果不为空那么说明他已经买过了,我们要在原来数量的基础上加1
   else
   {
    $_SESSION['myCart'][$id] += 1;
   }
}

/// <summary>
      /// 查看购物车按指定的模板购造HTML和数据
      /// 要把本方法输出的代码放到一个form表单中
      /// 当提交到转换处理页面时,可以从POST数组中得到
      /// 修改后的数量(input中的值)重新写入session变量中
      /// 然后立即重新导向本页面查看购物车表格会重新生成
      /// 这样可以正确的得到修改后的结果,
      /// 参考本类中的AlterGoodsCount方法
      /// </summary>
      /// <param name="$tempLate">HTML格式模板
      /// </param>
      /// <param name="$safeSql">基本的不带条件
      /// SQL</param>      
      /// <param name="$cashUrl">结账页面URL</param>
public function ShowCart($tempLate,$safeSql,$alterquantityUrl,$cashUrl)
{
   include($tempLate);//把模板包含进来
   $condition = "";//查询条件,准备拼接id参数
   ;
   //$testString = $myCart;
   $count = count($_SESSION['myCart']);
   
   //拼接SQL查询条件
   if($count == null)
   {
    echo "<td align='center' height='20'>您还没有挑选商品,去挑选您喜欢的商品吧!</td>";
   }
   else
   {
   for ($i = 0; $i < $count; $i++)
   {
//    $myarr = $dealCart->GetCart();
//    $key = key($myarr);
    //key()函数返回当前项的键值在这里就是商品ID
    $key = key($_SESSION['myCart']);   
    $condition .= " GoodsId=$key";
    if ($i < $count - 1)
    {
   $condition .= ' or ';
    }                        //用or连接多个条件
    next($_SESSION['myCart']);       //把指针在数组中推进
   }
   $safeSql .= $condition;
   $result = DBHelper::ExecuteCommand($safeSql);
   $cartItems = '';
   $total = 0;
   $textboxCount = 0; //计算textbox的数量用来拼接textboxname
   $inputCount = 0; //隐藏域中商品id的数量
   while ($row = mysql_fetch_array($result))
   {   
    $goodsId = $row['GoodsId'];
    $imgUrl = $row['GoodsPhopo'];
    $goodsName = $row['GoodsName'];
    $unitPrice = $row['GoodsPrice'];
    $goodsXingHao = $row['GoodsXingHao'];
    $goodsPinPai = $row['GoodsPinPai'];
    $goodsHui = $row['GoodsHuiyuanjia'];
    $goodsQuantity = $_SESSION['myCart'][$row['GoodsId']];
   $total += $unitPrice * $goodsQuantity;//计算总金额
    $textBoxName = 'textBox'.$textboxCount;//拼接textbox名字
    $inputName = 'input'.$inputCount;//拼接input名字
    ++$textboxCount;//每循环一次就将其加1
    ++$inputCount;
    $cartItems .= str_ireplace(
       array('{goodsId}','{imgUrl}','{goodsName}','{unitPrice}','{huiyuanPrice}','{GoodsXingHao}','{goodsPinPai}','{goodsQuantity}','{textBoxName}','{inputName}'),    array($goodsId,$imgUrl,$goodsName,$unitPrice,$goodsHui,$goodsXingHao,$goodsPinPai,$goodsQuantity,$textBoxName,$inputName),
       $cartContent
       );   
    }
   }
   
   $cartItems .= str_ireplace(
      array('{total}','{cashUrl}','{textBoxCount}','{inputCount}'),
      array($total,$cashUrl,$textboxCount,$inputCount),
      $cartTotalAndGoCash
      );
   
   echo $cartItems; //输出查看购物车HTML
}

/// <summary>
      /// 把指定的商品移除购物车
      /// </summary>
      /// <param name="$id">商品id</param>
public function RemoveFromCart($id)
{
   $isSuccessful = false;
   $offSet = 0;   //记录要删除的元素的位置,将从这里开始删除一个元素
   $isFind = false;
   $goodCount = count($_SESSION['myCart']);
   $i = 0;
   while ($i < $goodCount)
   {
    $key = key($_SESSION['myCart']);
    if($key == $id)
    {
   $isSuccessful = true;
   $isFind = true;
   break;//如果相等就break掉
    }
    ++$offSet;//如果上面的if条件不为真
    next($_SESSION['myCart']);
   }
  //   while (($key = key($_SESSION['myCart'])))
//   {
//    if($key == $id)
//    {
//   $isSuccessful = true;
//   $isFind = true;
//   break;//如果相等就break掉
//    }
//    ++$offSet;//如果上面的if条件不为真
//    next($_SESSION['myCart']);
//   }
   
if($isFind==true)
{
   unset($_SESSION['myCart'][$id]);
//array_splice($_SESSION['myCart'],$offSet,1);//从数组的$offSet处删掉一个元素
}
   return $isSuccessful;
}

/// <summary>
      /// 修改购物车中指定的商品数量
      /// </summary>
      /// <param name="$id">商品id</param>
public function AlterGoodsQuantity($id,$newQuantity)
{
   $isSuccessful = false;
   if(!empty($_SESSION['myCart'][$id]))
   {   
    if($newQuantity > 0)
    {
   $_SESSION['myCart'][$id] = $newQuantity;
   $isSuccessful = true;
    }
    if($newQuantity < 0)
    {
   $_SESSION['myCart'][$id] = (-1) * $newQuantity;
   $isSuccessful = true;
    }   
   }
   
//   $_SESSION['myCart'] = $this->arrCart;
   return $isSuccessful;
}

/// <summary>
      /// 清空购物车,要求在调用本方法前以经在客户端用
      ///Javascript脚本询问过用户是否确定清空,
      ///本方法一经调用购物车信息将全部删除
      /// </summary>
public function ClearCart()
{
   array_splice($_SESSION['myCart'],0);//从头开始删除所有购物车数组变量
}
}
?>
  
页: [1]
查看完整版本: PHP购物车类