php 购物车的例子

PHP购物车,cookie存购物车ID,db存购物车数据。
 //购物车session的产生代码 
复制代码
  1  if(!  $session && !  $scid) { 
  2  /*  
  3  session用来区别每一个购物车,相当于每个车的身份证号; 
  4  scid只用来标识一个购物车id号,可以看做是每个车的名字; 
  5  当该购物车的id和session值两者都不存在时,就产生一个新购物车 
  6  */ 
  7  $session =  md5( uniqid( rand())); 
  8  /*  
  9  产生一个唯一的购物车session号 
 10  rand()先产生个随机数,uniqid()再在该随机数的基础上产生一个独一无二的字符串,最后对该字符串进行md5 
 11  */ 
 12  SetCookie(scid,  $sessiontime() + 14400); 
 13  /*  
 14  设置该购物车cookie 
 15  变量名:scid(不知到这里是不是少了一个 $号呢?=》更正:scid要加“”) 
 16  变量值: $session 
 17  有效时间:当前时间+14400秒(4小时内) 
 18  关于setcookie函数的详细用法,大家还是参看php手册吧~ 
 19  */ 
 20 } 
 21  class Cart {  // 开始购物车类 
 22  function check_item(  $table$session$product) { 
 23  /*  
 24  查验物品(表名,session,物品) 
 25  */ 
 26  $query = SELECT * FROM  $table WHERE session=' $session' AND product=' $product' ; 
 27  /*  
 28  看一看'表'里该'购物车'中有没有该'产品' 
 29  即,该产品有没有已经放入购物车 
 30  */ 
 31  $result =  mysql_query$query); 
 32  if(!  $result) { 
 33  return 0; 
 34 } 
 35  /*  
 36  查询失败 
 37  */ 
 38  $numRows =  mysql_num_rows$result); 
 39  if$numRows == 0) { 
 40  return 0; 
 41  /*  
 42  若没有找到,则返回0 
 43  */ 
 44 }  else { 
 45  $row =  mysql_fetch_object$result); 
 46  return  $row->quantity; 
 47  /*  
 48  若找到,则返回该物品数量 
 49  这里有必要解释一下mysql_fetch_object函数(下面还会用到): 
 50  【mysql_fetch_object() 和 mysql_fetch_array() 类似,只有一点区别 - 返回一个对象而不是数组。】 
 51  上面这句话摘自php手册,说得应该很明白了吧~ 
 52  简单的说就是,取一条记录中的某个字段,应该用“->”而不是像数组一样用下标 
 53  */ 
 54 } 
 55 } 
 56  function add_item(  $table$session$product$quantity) { 
 57  /*  
 58  添加新物品(表名,session,物品,数量) 
 59  */ 
 60  $qty =  $this->check_item(  $table$session$product); 
 61  /*  
 62  调用上面那个函数,先检查该类物品有没有已经放入车中 
 63  */ 
 64  if$qty == 0) { 
 65  $query = INSERT INTO  $table (session, product, quantity) VALUES ; 
 66  $query .= (' $session', ' $product', ' $quantity') ; 
 67  mysql_query$query); 
 68  /* 若车中没有,则像车中添加该物品 */ 
 69 }  else { 
 70  $quantity +=  $qty// 若有,则在原有基础上增加数量 
 71  $query = UPDATE  $table SET quantity=' $quantity' WHERE session=' $session' AND ; 
 72  $query .= product=' $product' ; 
 73  mysql_query$query); 
 74  /*  
 75  并修改数据库 
 76  */ 
 77 } 
 78 } 
 79  function delete_item(  $table$session$product) { 
 80  /*  
 81  删除物品(表名,session,物品) 
 82  */ 
 83  $query = DELETE FROM  $table WHERE session=' $session' AND product=' $product' ; 
 84  mysql_query$query); 
 85  /*  
 86  删除该购物车中该类物品 
 87  */ 
 88 } 
 89  function modify_quantity(  $table$session$product$quantity) { 
 90  /*  
 91  修改物品数量(表名,session,物品,数量) 
 92  */ 
 93  $query = UPDATE  $table SET quantity=' $quantity' WHERE session=' $session' ; 
 94  $query .= AND product=' $product' ; 
 95  mysql_query$query); 
 96  /*  
 97  将该物品数量修改为参数中的值 
 98  */ 
 99 } 
100  function clear_cart(  $table$session) { 
101  /*  
102  清空购物车(没什么好说) 
103  */ 
104  $query = DELETE FROM  $table WHERE session=' $session' ; 
105  mysql_query$query); 
106 } 
107  function cart_total(  $table$session) { 
108  /*  
109  车中物品总价 
110  */ 
111  $query = SELECT * FROM  $table WHERE session=' $session' ; 
112  $result =  mysql_query$query); 
113  /*  
114  先把车中所有物品取出 
115  */ 
116  if( mysql_num_rows$result) > 0) { 
117  while$row =  mysql_fetch_object$result)) { 
118  /*  
119  如果物品数量>0个,则逐个判断价格并计算 
120  */ 
121  $query = SELECT price FROM inventory WHERE product=' $row->product' ; 
122  $invResult =  mysql_query$query); 
123  /*  
124  从inventory(库存)表中查找该物品的价格 
125  */ 
126  $row_price =  mysql_fetch_object$invResult); 
127  $total += (  $row_price->price *  $row->quantity); 
128  /*  
129  总价 += 该物品价格 * 该物品数量 
130  ( 大家应该能看明白吧:) ) 
131  */ 
132 } 
133 } 
134  return  $total// 返回总价钱 
135 
136  function display_contents(  $table$session) { 
137  /*  
138  获取关于车中所有物品的详细信息 
139  */ 
140  $count = 0; 
141  /*  
142  物品数量计数 
143  注意,该变量不仅仅为了对物品数量进行统计,更重要的是,它将作为返回值数组中的下标,用来区别每一个物品! 
144  */ 
145  $query = SELECT * FROM  $table WHERE session=' $session' ORDER BY id ; 
146  $result =  mysql_query$query); 
147  /*  
148  先取出车中所有物品 
149  */ 
150  while$row =  mysql_fetch_object$result)) { 
151  /*  
152  分别对每一个物品进行取详细信息 
153  */ 
154  $query = SELECT * FROM inventory WHERE product=' $row->product' ; 
155  $result_inv =  mysql_query$query); 
156  /*  
157  从inventory(库存)表中查找该物品的相关信息 
158  */ 
159  $row_inventory =  mysql_fetch_object$result_inv); 
160  $contents[product][  $count] =  $row_inventory->product; 
161  $contents[price][  $count] =  $row_inventory->price; 
162  $contents[quantity][  $count] =  $row->quantity; 
163  $contents[total][  $count] = (  $row_inventory->price *  $row->quantity); 
164  $contents[description][  $count] =  $row_inventory->description; 
165  /*  
166  把所有关于该物品的详细信息放入 $contents数组 
167  $contents是一个二维数组 
168  第一组下标是区别每个物品各个不同的信息(如物品名,价钱,数量等等) 
169  第二组下标是区别不同的物品(这就是前面定义的 $count变量的作用) 
170  */ 
171  $count++;  // 物品数量加一(即下一个物品) 
172 
173  $total =  $this->cart_total(  $table$session); 
174  $contents[ final] =  $total
175  /*  
176  同时调用上面那个cart_total函数,计算下总价钱 
177  并放入 $contents数组中 
178  */ 
179  return  $contents
180  /*  
181  将该数组返回 
182  */ 
183 } 
184  function num_items(  $table$session) { 
185  /*  
186  返回物品种类总数(也就是说,两个相同的东西算一种 好像是废话- -!) 
187  */ 
188  $query = SELECT * FROM  $table WHERE session=' $session' ; 
189  $result =  mysql_query$query); 
190  $num_rows =  mysql_num_rows$result); 
191  return  $num_rows
192  /*  
193  取出车中所有物品,获取该操作影响的数据库行数,即物品总数(没什么好说的) 
194  */ 
195 } 
196  function quant_items(  $table$session) { 
197  /*  
198  返回所有物品总数(也就是说,两个相同的东西也算两个物品 - -#) 
199  */ 
200  $quant = 0; //  物品总量 
201  $query = SELECT * FROM  $table WHERE session=' $session' ; 
202  $result =  mysql_query$query); 
203  while$row =  mysql_fetch_object$result)) { 
204  /*  
205  把每种物品逐个取出 
206  */ 
207  $quant +=  $row->quantity;  // 该物品数量加到总量里去 
208 
209  return  $quant// 返回总量 
210 
211 }
复制代码
>>>> 参考链接:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值