Home > Backend Development > PHP Tutorial > An example of php shopping cart implementation code (session mode)

An example of php shopping cart implementation code (session mode)

WBOY
Release: 2016-07-25 08:59:36
Original
1022 people have browsed it
  1. /**

  2. * php shopping cart
  3. * Edit bbs.it-home.org
  4. */

  5. //Shopping cart session generation code

  6. if(! $session && ! $ scid) {
  7. /*
  8. session is used to distinguish each shopping cart, which is equivalent to the ID number of each cart;
  9. scid is only used to identify a shopping cart ID number, which can be regarded as the name of each cart;
  10. when When both the id and session value of the shopping cart do not exist, a new shopping cart is generated
  11. */
  12. $session = md5(uniqid(rand()));
  13. /*
  14. Generates a unique shopping cart session number
  15. rand() first generates a random number, uniqid() then generates a unique string based on the random number, and finally performs md5 on the string
  16. */
  17. SetCookie(scid, $session, time() + 14400);
  18. /*
  19. Set the shopping cart cookie
  20. Variable name: scid (I wonder if there is a $ sign missing here? =》Correction: Add "" to scid)
  21. Variable value: $session
  22. Valid time: Current time + 14400 seconds (within 4 hours)
  23. For detailed usage of the setcookie function, please refer to the PHP manual~
  24. */
  25. }
  26. class Cart { //Start the shopping cart class
  27. function check_item( $table, $session, $product) {
  28. /*
  29. Check item (table name, session, item)
  30. */
  31. $query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
  32. /*
  33. Look Check whether the 'product' is in the 'shopping cart' in the 'table'
  34. That is, whether the product has been put into the shopping cart
  35. */
  36. $result = mysql_query( $query);
  37. if(! $result) {
  38. return 0;
  39. }
  40. /*
  41. Query failed
  42. */
  43. $numRows = mysql_num_rows( $result);
  44. if( $numRows == 0) {
  45. return 0;
  46. /*
  47. If not found, return 0
  48. */
  49. } else {
  50. $row = mysql_fetch_object( $result);
  51. return $row->quantity;
  52. /*
  53. If found, return the quantity of the item
  54. It is necessary to explain the mysql_fetch_object function here (also below) Will be used):
  55. [mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - it returns an object instead of an array.】
  56. To get a certain field in a record, you should use "->" instead of using subscripts like an array
  57. */
  58. }
  59. }
  60. function add_item( $table, $session, $product, $quantity) {
  61. /*
  62. Add new items (table name, session, item, quantity)
  63. */
  64. $qty = $this->check_item( $table, $session, $product);
  65. /*
  66. Call the above function, First check whether such items have been put into the car
  67. */
  68. if( $qty == 0) {
  69. $query = INSERT INTO $table (session, product, quantity) VALUES ;
  70. $query .= (' $ session', ' $product', ' $quantity') ;
  71. mysql_query( $query);
  72. /*If it is not in the car, add the item to the car*/
  73. } else {
  74. $quantity += $qty; //If there is, increase the quantity on the original basis
  75. $query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
  76. $query .= product=' $product' ;
  77. mysql_query( $query);
  78. /*
  79. And modify the database
  80. */
  81. }
  82. }
  83. function delete_item( $table, $session, $product) {
  84. /*
  85. Delete item (table name, session, item)
  86. */
  87. $query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
  88. mysql_query( $query);
  89. /*
  90. Delete this type of item in the php shopping cart
  91. */
  92. }
  93. function modify_quantity ($table, $session, $product, $quantity) {
  94. /*
  95. Modify item quantity (table name, session, item, quantity)
  96. */
  97. $query = UPDATE $table SET quantity=' $quantity' WHERE session =' $session' ;
  98. $query .= AND product=' $product' ;
  99. mysql_query( $query);
  100. /*
  101. Modify the item quantity to the value in the parameter
  102. */
  103. }
  104. function clear_cart( $ table, $session) {
  105. /*
  106. Clear the shopping cart (nothing to say)
  107. */
  108. $query = DELETE FROM $table WHERE session=' $session' ;
  109. mysql_query( $query);
  110. }
  111. function cart_total( $ table, $session) {
  112. /*
  113. Total price of items in the car
  114. */
  115. $query = SELECT * FROM $table WHERE session=' $session' ;
  116. $result = mysql_query( $query);
  117. /*
  118. First Take out all the items in the car
  119. */
  120. if(mysql_num_rows( $result) > 0) {
  121. while( $row = mysql_fetch_object( $result)) {
  122. /*
  123. If the number of items > 0, then judge one by one Price and calculate
  124. */
  125. $query = SELECT price FROM inventory WHERE product=' $row->product' ;
  126. $invResult = mysql_query( $query);
  127. /*
  128. Find the item from the inventory table The price of
  129. */
  130. $row_price = mysql_fetch_object( $invResult);
  131. $total += ( $row_price->price * $row->quantity);
  132. /*
  133. Total price+= price of the item * price of the item Quantity
  134. (Everyone should be able to understand it:) )
  135. */
  136. }
  137. }
  138. return $total; //Return the total price
  139. }
  140. function display_contents($table, $session) {
  141. /*
  142. Get information about Chezhong Detailed information of all items
  143. */
  144. $count = 0;
  145. /*
  146. Item quantity count
  147. Note that this variable is not only used to count the number of items, but more importantly, it will be used as a subscript in the return value array , used to distinguish each item!
  148. */
  149. $query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
  150. $result = mysql_query( $query);
  151. /*
  152. First take out all the items in the car
  153. */
  154. while( $ row = mysql_fetch_object( $result)) {
  155. /*
  156. Fetch detailed information for each item separately
  157. */
  158. $query = SELECT * FROM inventory WHERE product=' $row->product' ;
  159. $result_inv = mysql_query ( $query);
  160. /*
  161. Find related information about the item from the inventory table
  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. Put all the details about the item into $contents Array
  171. $contents is a two-dimensional array
  172. The first set of subscripts distinguishes the different information of each item (such as item name, price, quantity, etc.)
  173. The second set of subscripts distinguishes different items (this is the previous The function of the defined $count variable)
  174. */
  175. $count++; //The number of items plus one (i.e. the next item)
  176. }
  177. $total = $this->cart_total( $table, $session);
  178. $contents [final] = $total;
  179. /*
  180. Call the cart_total function above at the same time, calculate the total price
  181. and put it into the $contents array
  182. */
  183. return $contents;
  184. /*
  185. return the array
  186. */
  187. }
  188. function num_items( $table, $session) {
  189. /*
  190. Returns the total number of item types (that is, it seems nonsense to count two identical items as one - -!)
  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. Take out all the items in the car and get the ones affected by the operation The number of database rows, that is, the total number of items
  198. */
  199. }
  200. function quant_items($table, $session) {
  201. /*
  202. returns the total number of all items (that is, two identical items are also counted as two items - -#)
  203. */
  204. $quant = 0;//Total quantity of items
  205. $query = SELECT * FROM $table WHERE session=' $session' ;
  206. $result = mysql_query( $query);
  207. while( $row = mysql_fetch_object( $ result)) {
  208. /*
  209. Take out each item one by one
  210. */
  211. $quant += $row->quantity; //Add the quantity of the item to the total
  212. }
  213. return $quant; //Return Total amount
  214. }
  215. }
  216. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template