国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php購物車代碼

2024-05-04 21:48:59
字體:
供稿:網(wǎng)友

這里我們?yōu)槟闾峁﹤€(gè)簡單的php購物車代碼,從增加購物產(chǎn)品與發(fā)生購買了,在商城開發(fā)中,這個(gè)功能是少不了的,我們不需要數(shù)據(jù)庫,用了txt文本文件來操作用戶購物的內(nèi)容.

增加商品到購物車,代碼如下:

  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 "已經(jīng)注冊"; 
  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.         已經(jīng)添加 <?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.     商品單價(jià): RMB<?php echo $curr_product[price]; ?> 
  66.     <br> 
  67.     商品數(shù)量: <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.     //開源代碼Vevb.com 
  71.     <input type="submit" value="添加至購物欄"
  72.     </form> 
  73. <?php } ?> 
  74. </body> 
  75. </html> 

查看購物車的商品,代碼如下:

  1. <?php 
  2. // 
  3. // cart.php:  m.survivalescaperooms.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>歡迎進(jìn)入網(wǎng)上商店</h1> 
  31.  
  32. <?php 
  33. if ($_SESSION[cart][num_items]) {  // If there is something to show 
  34. ?> 
  35. <h2>當(dāng)前在購物籃里的物品</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.         單價(jià) 
  47.     </th> 
  48.     <th> 
  49.         數(shù)量 
  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="數(shù)量更改"
  83.         </form> 
  84.     </td> 
  85. </tr> 
  86. <?php 
  87.     } 
  88. ?> 
  89. <tr> 
  90.     <td colspan="2" ALIGN="right"
  91.        <b>合計(jì): </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.         單價(jià) 
  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> 

修改購物車的數(shù)量,代碼如下:

  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.         數(shù)量修改 
  33.     </title> 
  34. </head> 
  35. <body> 
  36.     <h1> 將數(shù)量: <?php echo $old_num; ?> 更改為 
  37.          <?php echo $_POST[quantity]; ?></h1> 
  38.     <a href="cart.php">返回</a> 商品列表頁面. 
  39. </body> 
  40. </html> 

功能頁面,用戶把購物車?yán)锩娴膬?nèi)容保存到txt數(shù)據(jù)庫,代碼如下:

  1. <?php 
  2. //物品數(shù)組 
  3. $master_products_list = array(); 
  4.  
  5.  
  6. //載入物品數(shù)據(jù)函數(shù) 
  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.     //讀取文件內(nèi)容 
  17.     while ($line = fgets($fp, 1024)) { 
  18.         list($id$name$desc$price) = explode('|'$line); //讀取每行數(shù)據(jù),數(shù)據(jù)以| 格開 
  19.         $id = trim($id); //去掉首尾特殊符號 
  20.         $master_products_list[$id] = array("name" =>  $name//名稱 
  21.                                            "desc" =>  $desc//說明 
  22.                                            "price" => $price); //單價(jià) 
  23.     } 
  24.  
  25.     @fclose($fp)  //關(guān)閉文件 
  26.         or die("關(guān)閉 $filename 文件失敗"); 
  27. ?> 

很簡單,我們只用了4個(gè)文件就實(shí)現(xiàn)用php 做好購物車功能,好了這只是一款簡單的php購物車代碼更復(fù)雜的需要考慮更多更好.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沁阳市| 安溪县| 上林县| 南阳市| 马关县| 红河县| 屏东市| 长岭县| 大关县| 瑞丽市| 武川县| 漳州市| 新邵县| 阿尔山市| 巴塘县| 深州市| 苏尼特左旗| 马鞍山市| 渝北区| 巴里| 砀山县| 阿拉善左旗| 栖霞市| 洛隆县| 开鲁县| 阿拉善右旗| 巧家县| 甘孜县| 邵东县| 博白县| 滕州市| 宿松县| 彭泽县| 五寨县| 武功县| 乌兰浩特市| 仁化县| 稻城县| 正阳县| 炎陵县| 新宁县|