再来说一说PHP购物车实现方法

http://blog.csdn.net/hello_katty/article/details/45362131

购物车应该是用处比较多的一个功能点,这里写了个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容


增加商品到购物车,代码如下:


[php]  view plain copy
  1. <?php   
  2.   
  3. //   
  4.   
  5. // add_item.php: http://www.lai18.com  
  6.   
  7. //  Add an item to the shopping cart.   
  8.   
  9. //   
  10.   
  11. session_start();   
  12.   
  13. if (session_is_registered('cart')) {   
  14.   
  15.     session_register('cart');   
  16.   
  17. }   
  18.   
  19.    
  20.   
  21. require 'lib.inc.php'// LoadProducts()   
  22.   
  23.    
  24.   
  25. LoadProducts(); // Load products in $master_products_list   
  26.   
  27.    
  28.   
  29. // Make $curr_product global   
  30.   
  31. $curr_product = array();   
  32.   
  33.    
  34.   
  35. // Loop through all the products and pull up the product   
  36.   
  37. // that we are interested in   
  38.   
  39.    
  40.   
  41. foreach ($master_products_list as $prod_id => $product) {   
  42.   
  43.     if (trim($prod_id) == trim($_GET[id])) {   
  44.   
  45.         $curr_product = $product;   
  46.   
  47.     }   
  48.   
  49. }   
  50.   
  51.    
  52.   
  53. // Register our session   
  54.   
  55. //session_register('cart');   
  56.   
  57. //if(session_is_registered('cart')) echo "已经注册";   
  58.   
  59.    
  60.   
  61. if ($_POST[ordered]) {  // If they have chosen the product   
  62.   
  63.    
  64.   
  65.     array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity]));   
  66.   
  67.     $_SESSION[cart][num_items] += $_POST[quantity];   
  68.   
  69. }   
  70.   
  71. ?>  
  72.   
  73. <html>   
  74.   
  75. <head>   
  76.   
  77.     <title>   
  78.   
  79.     <?php if ($_POST[ordered]) {  ?>   
  80.   
  81.         已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮   
  82.   
  83.     <?php } else {  ?>   
  84.   
  85.         添加 <?php echo $curr_product[name]; ?> 到您的购物篮   
  86.   
  87.     <?php } ?>   
  88.   
  89.     </title>   
  90.   
  91. </head>   
  92.   
  93. <body>   
  94.   
  95. <?php if ($_POST[ordered]) {  ?>   
  96.   
  97.     <h1><?php echo $curr_product[name]; ?>   
  98.   
  99.         添加至购物篮成功</h1>   
  100.   
  101.    
  102.   
  103.     <a href="cart.php">返回</a> 商品列表页面.   
  104.   
  105. <?php }  else {  ?>   
  106.   
  107.     <h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1>   
  108.   
  109.    
  110.   
  111.     <form action="<?php echo $PHP_SELF; ?>" method="post">   
  112.   
  113.     商品名称: <?php echo $curr_product[name]; ?>   
  114.   
  115.     <br>   
  116.   
  117.     商品说明: <?php echo $curr_product[desc]; ?>   
  118.   
  119.     <br>   
  120.   
  121.     商品单价: RMB<?php echo $curr_product[price]; ?>   
  122.   
  123.     <br>   
  124.   
  125.     商品数量: <input type="text" size="7" name="quantity">   
  126.   
  127.     <input type="hidden" name="id" value="<?php echo $_GET[id]; ?>">   
  128.   
  129.     <input type="hidden" name="ordered" value="1">   
  130.   
  131.     <input type="submit" value="添加至购物栏">   
  132.   
  133.     </form>   
  134.   
  135. <?php } ?>   
  136.   
  137. </body>   
  138.   
  139. </html>  

查看购物车的商品,代码如下:


[php]  view plain copy
  1. <?php   
  2.   
  3. //   
  4.   
  5. // cart.php: http://www.lai18.com  
  6.   
  7. //   
  8.   
  9. session_start();   
  10.   
  11.    
  12.   
  13. require 'lib.inc.php';   
  14.   
  15. //判断购物篮会话变量cart是否注册,不注册则注册cart变量   
  16.   
  17. if (session_is_registered('cart')) {   
  18.   
  19.     session_register('cart');   
  20.   
  21. }   
  22.   
  23.    
  24.   
  25. // 如果购物篮没有初始化,则初始化购物篮   
  26.   
  27. if (!isset($_SESSION[cart][num_items])) {   
  28.   
  29.     $_SESSION[cart] = array("num_items" => 0,   
  30.   
  31.                   "products"  => array());   
  32.   
  33. }   
  34.   
  35. // From site_lib.inc, Loads the $master_products_list array   
  36.   
  37. LoadProducts(); //载入物品列表   
  38.   
  39. ?>  
  40.   
  41. <html>   
  42.   
  43. <head>   
  44.   
  45.     <title>演示会话跟踪的购物篮程序</title>   
  46.   
  47. </head>   
  48.   
  49.    
  50.   
  51. <body>   
  52.   
  53.    
  54.   
  55. <h1>欢迎进入网上商店</h1>   
  56.   
  57.    
  58.   
  59. <?php   
  60.   
  61. if ($_SESSION[cart][num_items]) {  // If there is something to show   
  62.   
  63. ?>   
  64.   
  65. <h2>当前在购物篮里的物品</h2>   
  66.   
  67. <br>   
  68.   
  69. <table border="2" cellpadding="5" cellspacing="2">   
  70.   
  71. <tr>   
  72.   
  73.     <th>   
  74.   
  75.         商品名称   
  76.   
  77.     </th>   
  78.   
  79.     <th>   
  80.   
  81.         商品说明   
  82.   
  83.     </th>   
  84.   
  85.     <th>   
  86.   
  87.         单价   
  88.   
  89.     </th>   
  90.   
  91.     <th>   
  92.   
  93.         数量   
  94.   
  95.     </th>   
  96.   
  97.     <th>    
  98.   
  99.            
  100.   
  101.     </th>   
  102.   
  103. </tr>   
  104.   
  105. <?php   
  106.   
  107.      
  108.   
  109.     // Loop through the products   
  110.   
  111.     foreach ($_SESSION[cart][products] as $i => $product) {   
  112.   
  113.         $product_id = $product[0];   
  114.   
  115.         $quantity   = $product[1];   
  116.   
  117.    
  118.   
  119.         $total += $quantity *   
  120.   
  121.                   (double)$master_products_list[$product_id][price];   
  122.   
  123. ?>   
  124.   
  125. <tr>   
  126.   
  127.     <td>   
  128.   
  129.         <?php echo $master_products_list[$product_id][name]; ?>   
  130.   
  131.     </td>   
  132.   
  133.     <td>   
  134.   
  135.         <?php echo $master_products_list[$product_id][desc]; ?>   
  136.   
  137.     </td>   
  138.   
  139.     <td>   
  140.   
  141.         <?php echo $master_products_list[$product_id][price]; ?>   
  142.   
  143.     </td>   
  144.   
  145.     <td>   
  146.   
  147.         <form action="change_quant.php" method="post">   
  148.   
  149.         <input type="hidden" name="id" value="<?php echo $i; ?>">   
  150.   
  151.         <input type="text" size="3" name="quantity"   
  152.   
  153.                 value="<?php echo $quantity; ?>">   
  154.   
  155.     </td>   
  156.   
  157.     <td>   
  158.   
  159.         <input type="submit" value="数量更改">   
  160.   
  161.         </form>   
  162.   
  163.     </td>   
  164.   
  165. </tr>   
  166.   
  167. <?php   
  168.   
  169.     }   
  170.   
  171. ?>   
  172.   
  173. <tr>   
  174.   
  175.     <td colspan="2" ALIGN="right">   
  176.   
  177.        <b>合计: </b>   
  178.   
  179.     </td>   
  180.   
  181.     <td colspan="2">   
  182.   
  183.         RMB:<?php echo $total; ?>   
  184.   
  185.     </td>   
  186.   
  187.  <td> </td>   
  188.   
  189. </tr>   
  190.   
  191. </table>   
  192.   
  193. <br>   
  194.   
  195. <br>   
  196.   
  197. <?php   
  198.   
  199. }   
  200.   
  201. ?>   
  202.   
  203.    
  204.   
  205. <h2>商店待出售的商品</h2>   
  206.   
  207. <br>   
  208.   
  209. <i>   
  210.   
  211.     我们提供以下商品待售:   
  212.   
  213. </i>   
  214.   
  215. <br>   
  216.   
  217. <table border="2" cellpadding="5" cellspacing="2">   
  218.   
  219. <tr>   
  220.   
  221.     <th>   
  222.   
  223.         商品名称   
  224.   
  225.     </th>   
  226.   
  227.     <th>   
  228.   
  229.         商品说明   
  230.   
  231.     </th>   
  232.   
  233.     <th>   
  234.   
  235.         单价   
  236.   
  237.     </th>   
  238.   
  239.     <th>    
  240.   
  241.            
  242.   
  243.     </th>   
  244.   
  245. </tr>   
  246.   
  247. <?php   
  248.   
  249.     // Show all of the products   
  250.   
  251.     foreach ($master_products_list as $product_id => $item) {   
  252.   
  253. ?>   
  254.   
  255. <tr>   
  256.   
  257.     <td>   
  258.   
  259.         <?php echo $item[name]; ?>   
  260.   
  261.     </td>   
  262.   
  263.     <td>   
  264.   
  265.         <?php echo $item[desc]; ?>   
  266.   
  267.     </td>   
  268.   
  269.     <td>   
  270.   
  271.         $<?php echo $item[price]; ?>   
  272.   
  273.     </td>   
  274.   
  275.     <td>   
  276.   
  277.         <a href="add_item.php?id=<?php echo $product_id; ?>">   
  278.   
  279.             添加至购物篮   
  280.   
  281.         </a>   
  282.   
  283.     </td>   
  284.   
  285. </tr>   
  286.   
  287. <?php   
  288.   
  289.     }   
  290.   
  291.    
  292.   
  293. ?>   
  294.   
  295. </table>  


修改购物车的数量,代码如下

[php]  view plain copy
  1. <?php   
  2.   
  3. //   
  4.   
  5. // change_quant.php: http://www.lai18.com  
  6.   
  7. //   Change the quantity of an item in the shopping cart.   
  8.   
  9. //   
  10.   
  11. session_start();   
  12.   
  13. if (session_is_registered('cart')) {   
  14.   
  15.     session_register('cart');   
  16.   
  17. }   
  18.   
  19.    
  20.   
  21. // Typecast to int, making sure we access the   
  22.   
  23. // right element below   
  24.   
  25. $i = (int)$_POST[id];   
  26.   
  27.    
  28.   
  29. // Save the old number of products for display   
  30.   
  31. // and arithmetic   
  32.   
  33. $old_num = $_SESSION[cart][products][$i][1];   
  34.   
  35.    
  36.   
  37. if ($_POST[quantity]) {   
  38.   
  39.     $_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity   
  40.   
  41. else {   
  42.   
  43.     unset($_SESSION[cart][products][$i]); // Send the product into oblivion   
  44.   
  45. }   
  46.   
  47.    
  48.   
  49. // Update the number of items   
  50.   
  51. $_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ?   
  52.   
  53.                    $_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) :   
  54.   
  55.                    $_SESSION[cart][num_items] + ($_POST[quantity]-$old_num);   
  56.   
  57. ?>   
  58.   
  59.    
  60.   
  61. <html>   
  62.   
  63. <head>   
  64.   
  65.     <title>   
  66.   
  67.         数量修改   
  68.   
  69.     </title>   
  70.   
  71. </head>   
  72.   
  73. <body>   
  74.   
  75.     <h1> 将数量: <?php echo $old_num; ?> 更改为   
  76.   
  77.          <?php echo $_POST[quantity]; ?></h1>   
  78.   
  79.     <a href="cart.php">返回</a> 商品列表页面.   
  80.   
  81. </body>   
  82.   
  83. </html>  


功能页面,用户把购物车里面的内容保存到txt数据库,代码如下:


[php]  view plain copy
  1. <?php   
  2.   
  3. //物品数组  http://www.lai18.com  
  4.   
  5. $master_products_list = array();   
  6.   
  7.    
  8.   
  9.    
  10.   
  11. //载入物品数据函数   
  12.   
  13. function LoadProducts() {   
  14.   
  15.     global $master_products_list;   
  16.   
  17.     $filename = 'products.txt';   
  18.   
  19.    
  20.   
  21.     $fp = @fopen($filename"r")   
  22.   
  23.         or die("打开 $filename 文件失败");   
  24.   
  25.     @flock($fp, 1)   
  26.   
  27.         or die("锁定 $filename 文件失败");   
  28.   
  29.    
  30.   
  31.     //读取文件内容   
  32.   
  33.     while ($line = fgets($fp, 1024)) {   
  34.   
  35.         list($id$name$desc$price) = explode('|'$line); //读取每行数据,数据以| 格开   
  36.   
  37.         $id = trim($id); //去掉首尾特殊符号   
  38.   
  39.         $master_products_list[$id] = array("name" =>  $name//名称   
  40.   
  41.                                            "desc" =>  $desc//说明   
  42.   
  43.                                            "price" => $price); //单价   
  44.   
  45.     }   
  46.   
  47.    
  48.   
  49.     @fclose($fp)  //关闭文件   
  50.   
  51.         or die("关闭 $filename 文件失败");   
  52.   
  53. }   
  54.   
  55. ?>  


用了4个文件就实现用php 做好购物车功能,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好. 

这个示例可对初学者有很好的借鉴意义,欢迎交流: http://www.lai18.com/content/368947.html 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值