当前位置:首页 > PHP教程 > php高级应用 > 列表

php购物车代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 16:01:09 浏览: 评论:0 

这里我们为你提供个简单的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.  
  11. require 'lib.inc.php'; // LoadProducts() 
  12.  
  13. LoadProducts(); // Load products in $master_products_list 
  14.  
  15. // Make $curr_product global 
  16. $curr_product = array(); 
  17.  
  18. // Loop through all the products and pull up the product 
  19. // that we are interested in 
  20.  
  21.  
  22.  
  23. foreach ($master_products_list as $prod_id => $product) { 
  24.     if (trim($prod_id) == trim($_GET[id])) { 
  25.         $curr_product = $product; 
  26.     } 
  27. } 
  28.  
  29.  
  30. // Register our session 
  31. //session_register('cart'); 
  32. //if(session_is_registered('cart')) echo "已经注册"; 
  33.  
  34.  
  35.  
  36. if ($_POST[ordered]) {  // If they have chosen the product 
  37.  
  38.     array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity])); 
  39.     $_SESSION[cart][num_items] += $_POST[quantity]; 
  40.  
  41. } 
  42. ?> 
  43.  
  44. <html> 
  45. <head> 
  46.     <title> 
  47.     <?php if ($_POST[ordered]) {  ?> 
  48.         已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮 
  49.     <?php } else {  ?> 
  50.         添加 <?php echo $curr_product[name]; ?> 到您的购物篮 
  51.     <?php } ?> 
  52.     </title> 
  53. </head> 
  54. <body> 
  55. <?php if ($_POST[ordered]) {  ?> 
  56.     <h1><?php echo $curr_product[name]; ?> 
  57.         添加至购物篮成功</h1> 
  58.  
  59.     <a href="cart.php">返回</a> 商品列表页面. 
  60. <?php }  else {  ?> 
  61.     <h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1> 
  62.  
  63.     <form action="<?php echo $PHP_SELF; ?>" method="post"> 
  64.     商品名称: <?php echo $curr_product[name]; ?> 
  65.     <br> 
  66.     商品说明: <?php echo $curr_product[desc]; ?> 
  67.     <br> 
  68.     商品单价: RMB<?php echo $curr_product[price]; ?> 
  69.     <br> 
  70.     商品数量: <input type="text" size="7" name="quantity"> 
  71.     <input type="hidden" name="id" value="<?php echo $_GET[id]; ?>"> 
  72.     <input type="hidden" name="ordered" value="1"> 
  73.     //开源代码phpfensi.com 
  74.     <input type="submit" value="添加至购物栏"> 
  75.     </form> 
  76. <?php } ?> 
  77. </body> 
  78. </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. // 如果购物篮没有初始化,则初始化购物篮 
  15. if (!isset($_SESSION[cart][num_items])) { 
  16.     $_SESSION[cart] = array("num_items" => 0, 
  17.                   "products"  => array()); 
  18. } 
  19.  
  20.  
  21. // From site_lib.inc, Loads the $master_products_list array 
  22. LoadProducts(); //载入物品列表 
  23. ?> 
  24.  
  25. <html> 
  26. <head> 
  27.     <title>演示会话跟踪的购物篮程序</title> 
  28. </head> 
  29.  
  30. <body> 
  31.  
  32. <h1>欢迎进入网上商店</h1> 
  33.  
  34. <?php 
  35. if ($_SESSION[cart][num_items]) {  // If there is something to show 
  36. ?> 
  37. <h2>当前在购物篮里的物品</h2> 
  38. <br> 
  39. <table border="2" cellpadding="5" cellspacing="2"> 
  40. <tr> 
  41.     <th> 
  42.         商品名称 
  43.     </th> 
  44.     <th> 
  45.         商品说明 
  46.     </th> 
  47.     <th> 
  48.         单价 
  49.     </th> 
  50.     <th> 
  51.         数量 
  52.     </th> 
  53.     <th>&nbsp; 
  54.          
  55.     </th> 
  56. </tr> 
  57. <?php 
  58.    
  59.     // Loop through the products 
  60.     foreach ($_SESSION[cart][products] as $i => $product) { 
  61.         $product_id = $product[0]; 
  62.         $quantity   = $product[1]; 
  63.  
  64.         $total += $quantity * 
  65.                   (double)$master_products_list[$product_id][price]; 
  66. ?> 
  67. <tr> 
  68.     <td> 
  69.         <?php echo $master_products_list[$product_id][name]; ?> 
  70.     </td> 
  71.     <td> 
  72.         <?php echo $master_products_list[$product_id][desc]; ?> 
  73.     </td> 
  74.     <td> 
  75.         <?php echo $master_products_list[$product_id][price]; ?> 
  76.     </td> 
  77.     <td> 
  78.         <form action="change_quant.php" method="post"> 
  79.         <input type="hidden" name="id" value="<?php echo $i; ?>"> 
  80.         <input type="text" size="3" name="quantity" 
  81.                 value="<?php echo $quantity; ?>"> 
  82.     </td> 
  83.     <td> 
  84.         <input type="submit" value="数量更改"> 
  85.         </form> 
  86.     </td> 
  87. </tr> 
  88. <?php 
  89.     } 
  90. ?> 
  91. <tr> 
  92.     <td colspan="2" ALIGN="right"> 
  93.        <b>合计: </b> 
  94.     </td> 
  95.     <td colspan="2"> 
  96.         RMB:<?php echo $total; ?> 
  97.     </td> 
  98.  <td>&nbsp;</td> 
  99. </tr> 
  100. </table> 
  101. <br> 
  102. <br> 
  103. <?php 
  104. } 
  105. ?> 
  106.  
  107. <h2>商店待出售的商品</h2> 
  108. <br> 
  109. <i> 
  110.     我们提供以下商品待售: 
  111. </i> 
  112. <br> 
  113. <table border="2" cellpadding="5" cellspacing="2"> 
  114. <tr> 
  115.     <th> 
  116.         商品名称 
  117.     </th> 
  118.     <th> 
  119.         商品说明 
  120.     </th> 
  121.     <th> 
  122.         单价 
  123.     </th> 
  124.     <th>&nbsp; 
  125.          
  126.     </th> 
  127. </tr> 
  128. <?php 
  129.     // Show all of the products 
  130.     foreach ($master_products_list as $product_id => $item) { 
  131. ?> 
  132. <tr> 
  133.     <td> 
  134.         <?php echo $item[name]; ?> 
  135.     </td> 
  136.     <td> 
  137.         <?php echo $item[desc]; ?> 
  138.     </td> 
  139.     <td> 
  140.         $<?php echo $item[price]; ?> 
  141.     </td> 
  142.     <td> 
  143.         <a href="add_item.php?id=<?php echo $product_id; ?>"> 
  144.             添加至购物篮 
  145.         </a> 
  146.     </td> 
  147. </tr> 
  148. <?php 
  149.     } 
  150.  
  151. ?> 
  152. </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.  
  11. // Typecast to int, making sure we access the 
  12. // right element below 
  13. $i = (int)$_POST[id]; 
  14.  
  15. // Save the old number of products for display 
  16. // and arithmetic 
  17. $old_num = $_SESSION[cart][products][$i][1]; 
  18.  
  19. if ($_POST[quantity]) { 
  20.     $_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity 
  21. } else { 
  22.     unset($_SESSION[cart][products][$i]); // Send the product into oblivion 
  23. } 
  24.  
  25. // Update the number of items 
  26. $_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ? 
  27.                    $_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) : 
  28.                    $_SESSION[cart][num_items] + ($_POST[quantity]-$old_num); 
  29. ?> 
  30.  
  31. <html> 
  32. <head> 
  33.     <title> 
  34.         数量修改 
  35.     </title> 
  36. </head> 
  37. <body> 
  38.     <h1> 将数量: <?php echo $old_num; ?> 更改为 
  39.          <?php echo $_POST[quantity]; ?></h1> 
  40.     <a href="cart.php">返回</a> 商品列表页面. 
  41. </body> 
  42. </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. } 
  28. ?> 

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

Tags: php购物车 php购物车实例

分享到: