购物车,就是在session中存放一个购物车类,然后通过操作这个类来对商品信息进行操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
<?php //使用session必须先开启session session_start(); class CartTool { //用于存放实例化后的本对象 private static $ins=null; //此数组存放商品信息 private $items=array(); function __construct() { # code... } function __clone(){ } //获取实例化后的类 public static function getIns(){ if (!(self::$ins instanceof self)) { self::$ins=new self(); } return self::$ins; } public static function getCart(){ //检查购物车session是否存在 //检查这个购物车session存放的是否本类 if (!isset($_SESSION['cart']) || !($_SESSION['cart'] instanceof self)) { //把购物车类放进session中 $_SESSION['cart']=self::getIns(); } return $_SESSION['cart']; } /** * 向购物车添加商品 * @param integer $id 商品的id * @param string $sn 商品的编号 * @param string $name 商品名称 * @param integer $price 商品价格 * @param integer $num 商品个数 */ public function add($id,$sn,$name,$price,$num=1){ $item=array(); $item['sn']=$sn; $item['name']=$name; $item['price']=$price; $item['num']=$num; //items是一个二维数组,购物车数组 //item是一个商品的信息(商品名字,价格,个数,等等)的一维数组, $this->items[$id]=$item; return true; } //清空购物车 public function clear(){ $this->items=array(); } //检查对应id的商品是否存在 public function isExist($id){ return array_key_exists($id,$this->items); } //更改购买商品的数目 public function changeNum($id,$num=1){ //检查对应的商品是否存在 if (!$this->isExist($id)) { //如果不存在,就此结束 return false; } //把接收的参数放到对应商品的数组中键值为数目(num)中 $this->items[$id]['num']=$num; return true; } //商品一个一个的加 public function plusNum($id){ if (!$this->isExist($id)) { return false; } $this->items[$id]['num']+=1; return true; } //商品一个一个的减 public function minusNum($id){ if (!$this->isExist($id)) { return false; } if ($this->items[$id]['num']>1) { $this->items[$id]['num']-=1; return true; }else{ return false; } } //删除单个商品 public function deleteItems($id){ if (!$this->isExist($id)) { return false; } unset($this->items[$id]); return true; } //购物车所有商品的种类 public function totalType(){ return count($this->items); } //购物车所有商品的总数 public function totalNum(){ if ($this->totalType()==0) { return 0; } $sum=0; foreach ($this->items as $v) { $sum+=$v['num']; } return $sum; } //购物车所有商品的总价格 public function totalPrice(){ if ($this->totalType()==0) { return 0; } $price=0.0; foreach ($this->items as $v) { $price+=$v['price']*$v['num']; } return $price; } //返回购物车所有商品 public function totalItems(){ return $this->items; } } /*echo '<pre>'; print_r(CartTool::getCart()); echo '</pre>';*/ $cart=CartTool::getCart(); /*if($cart->add(1,'001','手机',1000.77,1)){ echo 'OK'; } if($cart->add(2,'002','诺基亚',2000.56,1)){ echo 'OK'; }*/ //$cart->clear(); // //$cart->changeNum(3,2); // //$cart->plusNum(1); //$cart->minusNum(2); //echo $cart->deleteItems(1); echo '种类共有:'.$cart->totalType().'<br/>'; echo '商品总数共有:'.$cart->totalNum().'<br/>'; echo '商品总价格:'.$cart->totalPrice().'<br/>'; echo '所有商品:'.$cart->totalItems().'<br/>'; echo '<pre>'; print_r($cart); echo '</pre>'; ?> |