1 <?php
2 if(!isset($_SESSION))
3 session_start();
4 //分析购物车
5 //整站范围内,购物车全局有效
6 //技术 : session购物车 + 单例模式
7 //增删商品,判断商品是否存在
8 //查询商品种类,数量,查询总金额,返回购物车所有商品
9 //某商品数量+1 -1
10 class CartTool{
11 private static $ins = null; //实例保存
12 private $items = array(); //保存商品
13 //public $sign = 0;
14 //单例
15 protected function __construct(){
16 // $this->sign = mt_rand(1,10000);
17
18 }
19 //防止外界克隆
20 final protected function __clone(){
21 }
22 //单例方法,获取实例
23 protected function getIns(){
24 if(!(self::$ins instanceof self)){ //若不是自身的实例
25 self::$ins = new self();
26 }
27 return self::$ins;
28 }
29 //把购物车的单例对象放到session里
30 public static function getCart(){
31 //session中没有购物车,或者session总的实例不是本身的实例
32 if((!(isset($_SESSION['cart'])) || !($_SESSION['cart'] instanceof self))){
33 $_SESSION['cart'] = self::getIns();
34 }
35 return $_SESSION['cart'];
36 }
37 //添加商品
38 //id: goods_id goods_name shop_price number
39 public function addItem($id,$name,$price,$num = 1){
40 if(($this->hasItem($id))){ //若商品已存在,直接加数量
41 $this->incNum($id,$num);
42 return true;
43 }
44 $item_temp['id'] = $id;
45 $item_temp['name'] = $name;
46 $item_temp['price'] = $price;
47 $item_temp['num'] = $num;
48 $this->items["$id"] = $item_temp; //二维数组
49 }
50 //清空购物车
51 public function clearItem(){
52 $this->items = array();
53 }
54
55 //判断某商品是否存在
56 public function hasItem($id){
57 return array_key_exists($id,$this->items);
58 }
59 //修改购物车中的商品数量 int goods_id $num number
60 public function modNum($id,$num=1){
61 if(!($this->hasItem($id))){
62 echo "商品主键不存在,无法修改商品数量";
63 return false;
64 }
65 $this->items[$id]['num'] = $num;
66 }
67 //商品数量加一
68 public function incNum($id){
69 if(!($this->hasItem($id))){
70 echo "商品主键不存在,无法增加商品";
71 return false;
72 }
73 $this->items[$id]['num'] += 1;
74 }
75 //商品数量减一
76 public function decNum($id,$num=1){
77 if(!($this->hasItem($id))){
78 echo "商品主键不存在,无法减少商品";
79 return false;
80 }
81
82 $this->items[$id]['num'] -= $num;
83 //如果减少后商品数量为0了,则把商品从购物车删除
84 if($this->items[$id]['num'] <= 0)
85 $this->delItem($id);
86 }
87 //删除商品 id
88 public function delItem($id){
89 unset($this->items[$id]);
90 }
91 //查询购物车中商品种类
92 public function getCnt(){
93 return count($this->items);
94 }
95 //查询购物车总商品的个数
96 public function getNum(){
97 $temp_num = 0;
98 if($this->getCnt() == 0)//若购物车没有商品,数量自然为0
99 $temp_num = 0;
100 foreach($this->items as $item){
101 $temp_num += $item['num'];
102 }
103 return $temp_num;
104 }
105 //查询购物车总商品的总金额
106 public function getPrice(){
107 $temp_money = 0.0;
108 if($this->getCnt() == 0)//若购物车没有商品,金额自然为0
109 $temp_money = 0.0;
110 foreach($this->items as $item){
111 $temp_money += ($item['num'] * $item['price']);
112 }
113 return $temp_money;
114 }
115 //返回购物车所有商品,空:返回商品数组 任意字符串:数组形式返回所有商品的名字
116 public function getAllGoods($fun=''){
117 if($fun == '')
118 return $this->items;
119 //返回所有商品的名字
120 $temp_arr = array();
121 foreach($this->items as $k=>$v){
122 $temp_arr[] = array( $k=>$v['name'] );
123 }
124 return $temp_arr;
125 }
126 }
127 /** php 测试 代码
128 $cart = CartTool::getCart();
129 if(isset($_GET['test']) && $_GET['test'] == 'add'){
130 $cart->addItem('1',"雪",'88888');
131 $cart->addItem('5',"雪1",'88888');
132 $cart->addItem('8',"雪2",'88888');
133 echo 'add ok';
134 }else if(isset($_GET['test']) && $_GET['test'] == 'clear'){
135 $cart->clearItem();
136 echo "successufl ok";
137 }else if(isset($_GET['test']) && $_GET['test'] == 'num'){
138 $cart->modNum(8,3);
139 echo "modify ok";
140 }else if(isset($_GET['test']) && $_GET['test'] == 'add1'){
141 $cart->incNum(8);
142 echo "modify ok";
143 }else if(isset($_GET['test']) && $_GET['test'] == 'dec1'){
144 $cart->decNum(8);
145 echo "modify ok";
146 }else if(isset($_GET['test']) && $_GET['test'] == 'price'){
147 echo "总共",$cart->getNum(),"种商品";
148 $pri = $cart->getPrice();
149 echo "总共",$pri,'元';
150 }else if(isset($_GET['test']) && $_GET['test'] == 'all'){
151
152 print_r( $cart->getAllGoods());
153 echo "<hr/>";
154 print_r( $cart->getAllGoods(1));
155 }else{
156 print_r($cart);
157 }
158 echo "<br/>";
159 //print_r(CartTool::getCart());
160 */
161 ?>