国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP購物車用法之更新購物車數量

2024-05-04 21:49:17
字體:
來源:轉載
供稿:網友

購物車數量意思就是在我們買東西時可以隨時刪除工增加商品,這樣我們的購物車就必須更新記錄啊,下面我來給大家介紹PHP購物車更新購物車數量程序與原因,有需要了解的朋友可參考.

表單部分,代碼如下:

  1. <form action="?action=edit_num" method="post" name="car<?php $c_rs['id'];?>" id="car<?php $c_rs['id'];?>"> 
  2. <input name="suliang[<?php echo $c_rs['sp_id'];?>]" type="text" id="suliang[<?php echo $c_rs['sp_id'];?>]" value="<?php echo $c_rs['suliang'];?>"/> 
  3. <input type="submit" name="button" id="button" value="更新購物車" /> 
  4. </form> 

PHP處理部分,代碼如下:

  1. <?php 
  2. require 'config.inc.php'
  3. require 'checklogin.php'
  4. $username = $_SESSION['username']; 
  5. $action = $_GET['action']; 
  6. switch ($action) { 
  7. case "edit_num"
  8. $arr = $arr = $_POST['suliang']; 
  9. foreach($arr as $key=>$value){ 
  10. $sqlgx = "update `cartemp` set suliang='$value' where username='".$username."' and flag=0 and sp_id='".$key."'"
  11. mysql_query($sqlgx$conn); 
  12. echo "<script>location.href='shopcat.php'</script>"
  13. break
  14. case "null"
  15. $null_sql = "delete from `cartemp` where username='$username' and flag=0 ";//開源代碼Vevb.com 
  16. mysql_query($null_sql$conn); 
  17. echo "<script>location.href='shopcat.php'</script>"
  18. break
  19. case "del"
  20. $id = $_GET['id']; 
  21. $del_sql = "delete from `cartemp` where id=$id"
  22. mysql_query($del_sql$conn); 
  23. echo "<script>location.href='shopcat.php'</script>"
  24. break
  25. ?> 

上面全部使用了數據庫來操作,下面來個完全的類,代碼如下:

  1. class Cart { //開始購物車類  
  2. function check_item( $table$session$product) {  
  3. /*  
  4. 查驗物品(表名,session,物品)  
  5. */  
  6. $query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;  
  7. /*  
  8. 看一看'表'里該'購物車'中有沒有該'產品'  
  9. 即,該產品有沒有已經放入購物車  
  10. */  
  11. $result = mysql_query( $query);  
  12. if(! $result) {  
  13. return 0;  
  14. }  
  15. /*  
  16. 查詢失敗  
  17. */  
  18. $numRows = mysql_num_rows( $result);  
  19. if$numRows == 0) {  
  20. return 0;  
  21. /*  
  22. 若沒有找到,則返回0  
  23. */  
  24. else {  
  25. $row = mysql_fetch_object( $result);  
  26. return $row->quantity;  
  27. /*  
  28. 若找到,則返回該物品數量  
  29. 這里有必要解釋一下mysql_fetch_object函數(下面還會用到):  
  30. 【mysql_fetch_object() 和 mysql_fetch_array() 類似,只有一點區別 - 返回一個對象而不是數組。】  
  31. 上面這句話摘自php手冊,說得應該很明白了吧~  
  32. 簡單的說就是,取一條記錄中的某個字段,應該用“->”而不是像數組一樣用下標  
  33. */  
  34. }  
  35. }  
  36. function add_item( $table$session$product$quantity) {  
  37. /*  
  38. 添加新物品(表名,session,物品,數量)  
  39. */  
  40. $qty = $this->check_item( $table$session$product);  
  41. /*  
  42. 調用上面那個函數,先檢查該類物品有沒有已經放入車中  
  43. */  
  44. if$qty == 0) {  
  45. $query = INSERT INTO $table (session, product, quantity) VALUES ;  
  46. $query .= (' $session'' $product'' $quantity') ;  
  47. mysql_query( $query);  
  48. /*若車中沒有,則像車中添加該物品*/  
  49. else {  
  50. $quantity += $qty//若有,則在原有基礎上增加數量  
  51. $query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;  
  52. $query .= product=' $product' ;  
  53. mysql_query( $query);  
  54. /*  
  55. 并修改數據庫  
  56. */  
  57. }  
  58. }  
  59. function delete_item( $table$session$product) {  
  60. /*  
  61. 刪除物品(表名,session,物品)  
  62. */  
  63. $query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;  
  64. mysql_query( $query);  
  65. /*  
  66. 刪除該購物車中該類物品  
  67. */  
  68. }  
  69. function modify_quantity( $table$session$product$quantity) {  
  70. /*  
  71. 修改物品數量(表名,session,物品,數量)  
  72. */  
  73. $query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;  
  74. $query .= AND product=' $product' ;  
  75. mysql_query( $query);  
  76. /*  
  77. 將該物品數量修改為參數中的值  
  78. */  
  79. }  
  80. function clear_cart( $table$session) {  
  81. /*  
  82. 清空購物車(沒什么好說)  
  83. */  
  84. $query = DELETE FROM $table WHERE session=' $session' ;  
  85. mysql_query( $query);  
  86. }  
  87. function cart_total( $table$session) {  
  88. /*  
  89. 車中物品總價  
  90. */  
  91. $query = SELECT * FROM $table WHERE session=' $session' ;  
  92. $result = mysql_query( $query);  
  93. /*  
  94. 先把車中所有物品取出  
  95. */  
  96. if(mysql_num_rows( $result) > 0) {  
  97. while$row = mysql_fetch_object( $result)) {  
  98. /*  
  99. 如果物品數量>0個,則逐個判斷價格并計算  
  100. */  
  101. $query = SELECT price FROM inventory WHERE product=' $row->product' ;  
  102. $invResult = mysql_query( $query);  
  103. /*  
  104. 從inventory(庫存)表中查找該物品的價格  
  105. */  
  106. $row_price = mysql_fetch_object( $invResult);  
  107. $total += ( $row_price->price * $row->quantity);  
  108. /*  
  109. 總價 += 該物品價格 * 該物品數量  
  110. ( 大家應該能看明白吧:) )  
  111. */  
  112. }  
  113. }  
  114. return $total//返回總價錢  
  115. }  
  116. function display_contents( $table$session) {  
  117. /*  
  118. 獲取關于車中所有物品的詳細信息  
  119. */  
  120. $count = 0;  
  121. /*  
  122. 物品數量計數  
  123. 注意,該變量不僅僅為了對物品數量進行統計,更重要的是,它將作為返回值數組中的下標,用來區別每一個物品!  
  124. */  
  125. $query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;  
  126. $result = mysql_query( $query);  
  127. /*  
  128. 先取出車中所有物品  
  129. */  
  130. while$row = mysql_fetch_object( $result)) {  
  131. /*  
  132. 分別對每一個物品進行取詳細信息  
  133. */  
  134. $query = SELECT * FROM inventory WHERE product=' $row->product' ;  
  135. $result_inv = mysql_query( $query);  
  136. /*  
  137. 從inventory(庫存)表中查找該物品的相關信息  
  138. */  
  139. $row_inventory = mysql_fetch_object( $result_inv);  
  140. $contents[product][ $count] = $row_inventory->product;  
  141. $contents[price][ $count] = $row_inventory->price;  
  142. $contents[quantity][ $count] = $row->quantity;  
  143. $contents[total][ $count] = ( $row_inventory->price * $row->quantity);  
  144. $contents[description][ $count] = $row_inventory->description;  
  145. /*  
  146. 把所有關于該物品的詳細信息放入 $contents數組  
  147. $contents是一個二維數組  
  148. 第一組下標是區別每個物品各個不同的信息(如物品名,價錢,數量等等)  
  149. 第二組下標是區別不同的物品(這就是前面定義的 $count變量的作用)  
  150. */  
  151. $count++; //物品數量加一(即下一個物品)  
  152. }  
  153. $total = $this->cart_total( $table$session);  
  154. $contents[final] = $total;  
  155. /*  
  156. 同時調用上面那個cart_total函數,計算下總價錢  
  157. 并放入 $contents數組中  
  158. */  
  159. return $contents;  
  160. /*  
  161. 將該數組返回  
  162. */  
  163. }  
  164. function num_items( $table$session) {  
  165. /*  
  166. 返回物品種類總數(也就是說,兩個相同的東西算一種 好像是廢話- -!)  
  167. */  
  168. $query = SELECT * FROM $table WHERE session=' $session' ;  
  169. $result = mysql_query( $query);  
  170. $num_rows = mysql_num_rows( $result);  
  171. return $num_rows;  
  172. /*  
  173. 取出車中所有物品,獲取該操作影響的數據庫行數,即物品總數(沒什么好說的)  
  174. */  
  175. }  
  176. function quant_items( $table$session) {  
  177. /*  
  178. 返回所有物品總數(也就是說,兩個相同的東西也算兩個物品 - -#)  
  179. */  
  180. $quant = 0;// 物品總量  
  181. $query = SELECT * FROM $table WHERE session=' $session' ;  
  182. $result = mysql_query( $query);  
  183. while$row = mysql_fetch_object( $result)) {  
  184. /*  
  185. 把每種物品逐個取出  
  186. */  
  187. $quant += $row->quantity; //該物品數量加到總量里去  
  188. }  
  189. return $quant//返回總量  
  190. }  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朝阳区| 叶城县| 武强县| 准格尔旗| 钦州市| 无极县| 简阳市| 肥城市| 莎车县| 体育| 繁峙县| 台中市| 青河县| 邵阳县| 洪泽县| 昭平县| 平原县| 安泽县| 张家口市| 香格里拉县| 隆尧县| 济宁市| 昌都县| 安宁市| 喀喇| 工布江达县| 云和县| 漳浦县| 庆元县| 阜新| 通海县| 林芝县| 鄂州市| 岢岚县| 荣昌县| 乡宁县| 五寨县| 友谊县| 蓬莱市| 博爱县| 闽清县|