Php shopping cart implementation code example (session mode)

Source: Internet
Author: User
Php shopping cart implementation code example (session mode)

  1. /**

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

  5. // Code for generating the shopping cart session

  6. If (! $ Session &&! $ Scid ){
  7. /*
  8. Session is used to differentiate each shopping cart, which is equivalent to the ID card number of each car;
  9. Scid is only used to identify a shopping cart ID, which can be considered as the name of each vehicle;
  10. When neither the id nor the session value of the shopping cart exists, a new shopping cart is generated.
  11. */
  12. $ Session = md5 (uniqid (rand ()));
  13. /*
  14. Generate 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 md5
  16. */
  17. SetCookie (scid, $ session, time () + 14400 );
  18. /*
  19. Set the shopping cart cookie
  20. Variable name: scid (I wonder if a $ number is missing here? = "Correction: add" "to the scid)
  21. Variable value: $ session
  22. Effective Time: current time + 14400 seconds (within 4 hours)
  23. For detailed usage of the setcookie function, refer to the php Manual ~
  24. */
  25. }
  26. Class Cart {// start shopping Cart class
  27. Function check_item ($ table, $ session, $ product ){
  28. /*
  29. Check items (table name, session, item)
  30. */
  31. $ Query = SELECT * FROM $ table WHERE session = '$ session' AND product =' $ product ';
  32. /*
  33. Check whether the 'product' is included in the 'shopping cart' in the 'table'
  34. That is, whether the product has been placed in 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 no value is found, 0 is returned.
  48. */
  49. } Else {
  50. $ Row = mysql_fetch_object ($ result );
  51. Return $ row-> quantity;
  52. /*
  53. If yes, the number of items is returned.
  54. Here it is necessary to explain the mysql_fetch_object function (which will be used below ):
  55. [Mysql_fetch_object () is similar to mysql_fetch_array (). there is only one difference-returning an object instead of an array .]
  56. To retrieve a field in a record, use "->" instead of subscript 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 to first check whether this type of item has been placed in 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 the vehicle does not exist, add the item to the vehicle */
  73. } Else {
  74. $ Quantity + = $ qty; // If yes, the quantity is increased based on the original quantity.
  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 an 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 class of items in the php shopping cart
  91. */
  92. }
  93. Function modify_quantity ($ table, $ session, $ product, $ quantity ){
  94. /*
  95. Modify the number of items (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. Change the number of items 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. Take out all items in the car first
  119. */
  120. If (mysql_num_rows ($ result)> 0 ){
  121. While ($ row = mysql_fetch_object ($ result )){
  122. /*
  123. If the number of items is greater than 0, the price is determined and calculated one by one.
  124. */
  125. $ Query = SELECT price FROM inventory WHERE product = '$ row-> product ';
  126. $ InvResult = mysql_query ($ query );
  127. /*
  128. Search for the price of this item from the inventory table
  129. */
  130. $ Row_price = mysql_fetch_object ($ invResult );
  131. $ Total + = ($ row_price-> price * $ row-> quantity );
  132. /*
  133. Total price + = price of the item * quantity of the item
  134. (You can see it clearly :))
  135. */
  136. }
  137. }
  138. Return $ total; // return the total price.
  139. }
  140. Function display_contents ($ table, $ session ){
  141. /*
  142. Get detailed information about all items in a car
  143. */
  144. $ Count = 0;
  145. /*
  146. Item count
  147. Note that this variable is used not only to count the number of items, but also as a subscript in the returned value array to distinguish each item!
  148. */
  149. $ Query = SELECT * FROM $ table WHERE session = '$ session' order by id;
  150. $ Result = mysql_query ($ query );
  151. /*
  152. First retrieve all items in the car
  153. */
  154. While ($ row = mysql_fetch_object ($ result )){
  155. /*
  156. Obtain detailed information for each item
  157. */
  158. $ Query = SELECT * FROM inventory WHERE product = '$ row-> product ';
  159. $ Result_inv = mysql_query ($ query );
  160. /*
  161. Search for information about this 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. Add all the details about the item to the $ contents array.
  171. $ Contents is a two-dimensional array
  172. The first subscripts are used to differentiate different information (such as the item name, price, and quantity) of each item)
  173. The second subscript distinguishes different items (this is the role of the $ count variable defined above)
  174. */
  175. $ Count ++; // The number of items plus one (that is, the next item)
  176. }
  177. $ Total = $ this-> cart_total ($ table, $ session );
  178. $ Contents [final] = $ total;
  179. /*
  180. Call the cart_total function above to calculate the total price.
  181. And put it in the $ contents array.
  182. */
  183. Return $ contents;
  184. /*
  185. Returns the array
  186. */
  187. }
  188. Function num_items ($ table, $ session ){
  189. /*
  190. Total number of returned item types (that is, two identical items are regarded as nonsense --!)
  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. Retrieve all items in the vehicle and obtain the number of database lines affected by this operation, 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 to say, two identical items are counted as two items --#)
  203. */
  204. $ Quant = 0; // total number of items
  205. $ Query = SELECT * FROM $ table WHERE session = '$ session ';
  206. $ Result = mysql_query ($ query );
  207. While ($ row = mysql_fetch_object ($ result )){
  208. /*
  209. Retrieve each item one by one
  210. */
  211. $ Quant + = $ row-> quantity; // add the number of items to the total quantity.
  212. }
  213. Return $ quant; // The total number of returned results.
  214. }
  215. }
  216. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.