网站地图    收藏   

主页 > php专栏 > php应用 >

php购物车代码 - php高级应用

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:

[导读] 这里我们为你提供个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容.增加商品到购物车...

php购物车代码

这里我们为你提供个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容.

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

  1. <?php 
  2. // 
  3. // add_item.php: 
  4. //  Add an item to the shopping cart. 
  5. // 
  6. session_start(); 
  7. if (session_is_registered('cart')) { 
  8.     session_register('cart'); 
  9.  
  10. require 'lib.inc.php'// LoadProducts() 
  11.  
  12. LoadProducts(); // Load products in $master_products_list 
  13.  
  14. // Make $curr_product global 
  15. $curr_product = array(); 
  16.  
  17. // Loop through all the products and pull up the product 
  18. // that we are interested in 
  19.  
  20.  
  21.  
  22. foreach ($master_products_list as $prod_id => $product) { 
  23.     if (trim($prod_id) == trim($_GET[id])) { 
  24.         $curr_product = $product
  25.     } 
  26.  
  27.  
  28. // Register our session 
  29. //session_register('cart'); 
  30. //if(session_is_registered('cart')) echo "已经注册"; 
  31.  
  32.  
  33.  
  34. if ($_POST[ordered]) {  // If they have chosen the product 
  35.  
  36.     array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity])); 
  37.     $_SESSION[cart][num_items] += $_POST[quantity]; 
  38.  
  39. ?> 
  40.  
  41. <html> 
  42. <head> 
  43.     <title> 
  44.     <?php if ($_POST[ordered]) {  ?> 
  45.         已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮 
  46.     <?php } else {  ?> 
  47.         添加 <?php echo $curr_product[name]; ?> 到您的购物篮 
  48.     <?php } ?> 
  49.     </title> 
  50. </head> 
  51. <body> 
  52. <?php if ($_POST[ordered]) {  ?> 
  53.     <h1><?php echo $curr_product[name]; ?> 
  54.         添加至购物篮成功</h1> 
  55.  
  56.     <a href="cart.php">返回</a> 商品列表页面. 
  57. <?php }  else {  ?> 
  58.     <h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1> 
  59.  
  60.     <form action="<?php echo $PHP_SELF; ?>" method="post"
  61.     商品名称: <?php echo $curr_product[name]; ?> 
  62.     <br> 
  63.     商品说明: <?php echo $curr_product[desc]; ?> 
  64.     <br> 
  65.     商品单价: RMB<?php echo $curr_product[price]; ?> 
  66.     <br> 
  67.     商品数量: <input type="text" size="7" name="quantity"
  68.     <input type="hidden" name="id" value="<?php echo $_GET[id]; ?>"
  69.     <input type="hidden" name="ordered" value="1"
  70.     //开源代码phpfensi.com 
  71.     <input type="submit" value="添加至购物栏"
  72.     </form> 
  73. <?php } ?> 
  74. </body> 
  75. </html> 

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

  1. <?php 
  2. // 
  3. // cart.php:  www.phpfensi.com 
  4. // 
  5. session_start(); 
  6.  
  7. require 'lib.inc.php'
  8. //判断购物篮会话变量cart是否注册,不注册则注册cart变量 
  9. if (session_is_registered('cart')) { 
  10.     session_register('cart'); 
  11.  
  12.  
  13. // 如果购物篮没有初始化,则初始化购物篮 
  14. if (!isset($_SESSION[cart][num_items])) { 
  15.     $_SESSION[cart] = array("num_items" => 0, 
  16.                   "products"  => array()); 
  17.  
  18.  
  19. // From site_lib.inc, Loads the $master_products_list array 
  20. LoadProducts(); //载入物品列表 
  21. ?> 
  22.  
  23. <html> 
  24. <head> 
  25.     <title>演示会话跟踪的购物篮程序</title> 
  26. </head> 
  27.  
  28. <body> 
  29.  
  30. <h1>欢迎进入网上商店</h1> 
  31.  
  32. <?php 
  33. if ($_SESSION[cart][num_items]) {  // If there is something to show 
  34. ?> 
  35. <h2>当前在购物篮里的物品</h2> 
  36. <br> 
  37. <table border="2" cellpadding="5" cellspacing="2"
  38. <tr> 
  39.     <th> 
  40.         商品名称 
  41.     </th> 
  42.     <th> 
  43.         商品说明 
  44.     </th> 
  45.     <th> 
  46.         单价 
  47.     </th> 
  48.     <th> 
  49.         数量 
  50.     </th> 
  51.     <th>&nbsp; 
  52.          
  53.     </th> 
  54. </tr> 
  55. <?php 
  56.    
  57.     // Loop through the products 
  58.     foreach ($_SESSION[cart][products] as $i => $product) { 
  59.         $product_id = $product[0]; 
  60.         $quantity   = $product[1]; 
  61.  
  62.         $total += $quantity * 
  63.                   (double)$master_products_list[$product_id][price]; 
  64. ?> 
  65. <tr> 
  66.     <td> 
  67.         <?php echo $master_products_list[$product_id][name]; ?> 
  68.     </td> 
  69.     <td> 
  70.         <?php echo $master_products_list[$product_id][desc]; ?> 
  71.     </td> 
  72.     <td> 
  73.         <?php echo $master_products_list[$product_id][price]; ?> 
  74.     </td> 
  75.     <td> 
  76.         <form action="change_quant.php" method="post"
  77.         <input type="hidden" name="id" value="<?php echo $i; ?>"
  78.         <input type="text" size="3" name="quantity" 
  79.                 value="<?php echo $quantity; ?>"
  80.     </td> 
  81.     <td> 
  82.         <input type="submit" value="数量更改"
  83.         </form> 
  84.     </td> 
  85. </tr> 
  86. <?php 
  87.     } 
  88. ?> 
  89. <tr> 
  90.     <td colspan="2" ALIGN="right"
  91.        <b>合计: </b> 
  92.     </td> 
  93.     <td colspan="2"
  94.         RMB:<?php echo $total; ?> 
  95.     </td> 
  96.  <td>&nbsp;</td> 
  97. </tr> 
  98. </table> 
  99. <br> 
  100. <br> 
  101. <?php 
  102. ?> 
  103.  
  104. <h2>商店待出售的商品</h2> 
  105. <br> 
  106. <i> 
  107.     我们提供以下商品待售: 
  108. </i> 
  109. <br> 
  110. <table border="2" cellpadding="5" cellspacing="2"
  111. <tr> 
  112.     <th> 
  113.         商品名称 
  114.     </th> 
  115.     <th> 
  116.         商品说明 
  117.     </th> 
  118.     <th> 
  119.         单价 
  120.     </th> 
  121.     <th>&nbsp; 
  122.          
  123.     </th> 
  124. </tr> 
  125. <?php 
  126.     // Show all of the products 
  127.     foreach ($master_products_list as $product_id => $item) { 
  128. ?> 
  129. <tr> 
  130.     <td> 
  131.         <?php echo $item[name]; ?> 
  132.     </td> 
  133.     <td> 
  134.         <?php echo $item[desc]; ?> 
  135.     </td> 
  136.     <td> 
  137.         $<?php echo $item[price]; ?> 
  138.     </td> 
  139.     <td> 
  140.         <a href="add_item.php?id=<?php echo $product_id; ?>"
  141.             添加至购物篮 
  142.         </a> 
  143.     </td> 
  144. </tr> 
  145. <?php 
  146.     } 
  147.  
  148. ?> 
  149. </table> 

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

  1. <?php 
  2. // 
  3. // change_quant.php: 
  4. //   Change the quantity of an item in the shopping cart. 
  5. // 
  6. session_start(); 
  7. if (session_is_registered('cart')) { 
  8.     session_register('cart'); 
  9.  
  10. // Typecast to int, making sure we access the 
  11. // right element below 
  12. $i = (int)$_POST[id]; 
  13.  
  14. // Save the old number of products for display 
  15. // and arithmetic 
  16. $old_num = $_SESSION[cart][products][$i][1]; 
  17.  
  18. if ($_POST[quantity]) { 
  19.     $_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity 
  20. else { 
  21.     unset($_SESSION[cart][products][$i]); // Send the product into oblivion 
  22.  
  23. // Update the number of items 
  24. $_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ? 
  25.                    $_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) : 
  26.                    $_SESSION[cart][num_items] + ($_POST[quantity]-$old_num); 
  27. ?> 
  28.  
  29. <html> 
  30. <head> 
  31.     <title> 
  32.         数量修改 
  33.     </title> 
  34. </head> 
  35. <body> 
  36.     <h1> 将数量: <?php echo $old_num; ?> 更改为 
  37.          <?php echo $_POST[quantity]; ?></h1> 
  38.     <a href="cart.php">返回</a> 商品列表页面. 
  39. </body> 
  40. </html> 

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

  1. <?php 
  2. //物品数组 
  3. $master_products_list = array(); 
  4.  
  5.  
  6. //载入物品数据函数 
  7. function LoadProducts() { 
  8.     global $master_products_list
  9.     $filename = 'products.txt'
  10.  
  11.     $fp = @fopen($filename"r"
  12.         or die("打开 $filename 文件失败"); 
  13.     @flock($fp, 1) 
  14.         or die("锁定 $filename 文件失败"); 
  15.  
  16.     //读取文件内容 
  17.     while ($line = fgets($fp, 1024)) { 
  18.         list($id$name$desc$price) = explode('|'$line); //读取每行数据,数据以| 格开 
  19.         $id = trim($id); //去掉首尾特殊符号 
  20.         $master_products_list[$id] = array("name" =>  $name//名称 
  21.                                            "desc" =>  $desc//说明 
  22.                                            "price" => $price); //单价 
  23.     } 
  24.  
  25.     @fclose($fp)  //关闭文件 
  26.         or die("关闭 $filename 文件失败"); 
  27. ?> 

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

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论