php 購物車的實現代碼一例(session方式)

來源:互聯網
上載者:User
  1. /**

  2. * php 購物車
  3. * Edit bbs.it-home.org
  4. */

  5. //購物車session的產生代碼

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

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.