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

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

實用的php購物車程序

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

以前有用過一個感覺不錯,不過看了這個感覺也很好,所以介紹給需要的朋友參考一下,實例代碼如下:

  1. <?php 
  2. //調(diào)用實例 
  3. require_once 'cart.class.php'
  4. session_start(); 
  5. if(!isset($_SESSION['cart'])) { 
  6.  $_SESSION['cart'] = new Cart; 
  7. $cart =& $_SESSION['cart']; 
  8.  
  9. if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='add') ){ 
  10.  $p = $_POST['p']; 
  11.  $items = $cart->add($p); 
  12. if( ($_GET['action']=='remove')&&($_GET['key']!="") ) { 
  13.  $items = $cart->remove($_GET['key']); 
  14.  
  15. if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='modi') ){ 
  16.  $key = $_POST['key']; 
  17.  $value = $_POST['value']; 
  18.  for($i=0;$i<count($key);$i  ){ 
  19.   $items = $cart->modi($key[$i],$value[$i]); 
  20.  } 
  21.  
  22. $items = $cart->getCart(); 
  23. //打印 
  24. echo "<table border=1>"
  25. setlocale(LC_MONETARY, 'it_IT'); 
  26. foreach($items as $item){ 
  27.  echo "<tr><form method="post" action="tmp.php">"
  28.  echo "<td>ID:".$item['ID']."<input type=hidden name=key[] value=".$item['ID'].">"
  29.  echo "<td>產(chǎn)品:".$item['name']; 
  30.  echo "<td>單價:".$item['price']; 
  31.  echo "<td><input type=text name=value[] value=".$item['count'].">"
  32.   $sum = $item['count']*$item['price']; 
  33.  echo "<td>合計:".round($sum,2); 
  34.  echo "<td><input type=button value='刪除' onclick="location='?action=remove&key=".$item['ID']."'">"
  35. echo "<input type=hidden name=action value=modi>"
  36. echo "<tr><td colspan=7><input type=submit />"
  37. echo "</td></form></tr></table>"
  38.  
  39.  
  40. ?> 
  41. <hr> 
  42. <form method="post" action="tmp.php"
  43. ID:<input type="text" name="p[]" /> 
  44. 品名:<input type="text" name="p[]" /> 
  45. 單價:<input type="text" name="p[]" /> 
  46. 數(shù)量:<input type="text" name="p[]" /> 
  47. <input type=hidden name=action value=add> 
  48. <input type="submit" /> 
  49. </form> 
  50.  
  51.  
  52.  
  53. <? 
  54. /** 
  55.  * Cart 
  56.  *  
  57.  * 購物車類 
  58.  *  
  59.  * @author  doodoo<pWtitle@yahoo.com.cn> 
  60.  * @package     Cart 
  61.  * @category    Cart 
  62.  * @license     PHP License 
  63.  * @access      public 
  64.  * @version     $Revision: 1.10 $ 
  65.  */ 
  66. Class Cart{ 
  67.  
  68.  var $cart
  69.  var $totalCount//商品總數(shù)量 
  70.  var $totalPrices//商品總金額 
  71.  
  72.   /** 
  73.      * Cart Constructor 
  74.      *  
  75.      * 類的構(gòu)造函數(shù),使購物車保持穩(wěn)定的初始化狀態(tài)  
  76.      *  
  77.      * @static  
  78.      * @access  public  
  79.      * @return  void   無返回值 
  80.      * @param   void   無參數(shù) 
  81.      */ 
  82.   function Cart(){ 
  83.   $this->totalCount = 0; 
  84.   $this->totalPrice = 0; 
  85.   $this->cart = array(); 
  86.  } 
  87.  
  88.  // }}} 
  89.     // {{{ add($item) 
  90.  
  91.     /** 
  92.  * 增加商品到當前購物車 
  93.  * 
  94.     * @access public 
  95.     * @param  array $item 商品信息(一維數(shù)組:array(商品ID,商品名稱,商品單價,商品數(shù)量)) 
  96.     * @return array   返回當前購物車內(nèi)商品的數(shù)組 
  97.     */ 
  98.  function add($item){ 
  99.   if(!is_array($item)||is_null($item)) return $this->cart; 
  100.   if(!is_numeric(end($item))||(!is_numeric(prev($item)))) { 
  101.    echo "價格和數(shù)量必須是數(shù)字"
  102.    return $this->cart; 
  103.   } 
  104.   reset($item); //這一句是必須的,因為上面的判斷已經(jīng)移動了數(shù)組的指標 
  105.   $key = current($item); 
  106.   if($key==""return $this->cart; 
  107.   if($this->_isExists($key)){  //商品是否已經(jīng)存在? 
  108.     $this->cart[$key]['count']  = end($item); 
  109.   return $this->cart; 
  110.   } 
  111.  
  112.   $this->cart[$key]['ID']  = $key
  113.   $this->cart[$key]['name'] = next($item); 
  114.   $this->cart[$key]['price'] = next($item); 
  115.   $this->cart[$key]['count'] = next($item); 
  116.  
  117.  return $this->cart; 
  118.  } 
  119.  
  120.  // }}} 
  121.     // {{{ add($item) 
  122.  
  123.     /** 
  124.  * 從當前購物車中取出部分或全部商品 
  125.  * 當 $key=="" 的時候,清空當前購物車 
  126.  * 當 $key!=""&&$count=="" 的時候,從當前購物車中揀出商品ID號為 $key 的全部商品  
  127.  * 當 $key!=""&&$count!="" 的時候,從當前購物車中揀出 $count個 商品ID號為 $key 的商品  
  128.  * 
  129.     * @access public 
  130.     * @param  string $key 商品ID 
  131.     * @return mixed   返回真假或當前購物車內(nèi)商品的數(shù)組 
  132.     */ 
  133.  function remove($key="",$count=""){ 
  134.   if($key=="") { 
  135.    $this->cart = array(); 
  136.    return true; 
  137.   } 
  138.   if(!array_key_exists($key,$this->cart)) return false; 
  139.   if($count==""){ //移去這一類商品 
  140.    unset($this->cart[$key]); 
  141.   }else//移去$count個商品 
  142.    $this->cart[$key]['count'] -= $count
  143.    if($this->cart[$key]['count']<=0) unset($this->cart[$key]); 
  144.   } 
  145.   return $this->cart; 
  146.  } 
  147.  
  148.  // }}} 
  149.     // {{{ modi($key,$value) 
  150.  
  151.     /** 
  152.  * 修改購物車內(nèi)商品ID為 $key 的商品的數(shù)量為 $value 
  153.  * 
  154.     * @access public 
  155.     * @param  string $key 商品ID 
  156.     * @param  int $value 商品數(shù)量 
  157.     * @return array  返回當前購物車內(nèi)商品的數(shù)組; 
  158.     */ 
  159.  function modi($key,$value){ 
  160.   if(!$this->_isExists($key)) return $this->cart();  //不存在此商品,直接返回 
  161.   if($value<=0){     // value 太小,全部刪除 
  162.    unset($this->cart[$key]); 
  163.    return $this->cart; 
  164.   } 
  165.   $this->cart[$key]['count'] = $value
  166.   return $this->cart; 
  167.  } 
  168.  
  169.  
  170.     /** 
  171.  * 返回當前購物車內(nèi)商品的數(shù)組 
  172.  * 
  173.     * @access public 
  174.     * @return array  返回當前購物車內(nèi)商品的數(shù)組; 
  175.     */ 
  176.  function getCart(){ 
  177.   return $this->cart; 
  178.  } 
  179.  
  180.  // }}} 
  181.     // {{{ _isExists($key) 
  182.  
  183.     /** 
  184.  * 判斷當前購物車中是否存在商品ID號為$key的商品 
  185.  * 
  186.     * @access private 
  187.     * @param  string $key 商品ID 
  188.     * @return bool   true or false; 
  189.     */ 
  190.     function _isExists($key
  191.     { 
  192.   if(isset($this->cart[$key])&&!emptyempty($this->cart[$key])&&array_key_exists($key,$this->cart)) 
  193.    return true; 
  194.     return false; 
  195.     } 
  196.  
  197.  // }}} 
  198.     // {{{ isEmpty() 
  199.  
  200.     /** 
  201.  * 判斷當前購物車是否為空,即沒有任何商品 
  202.  * 
  203.     * @access public 
  204.     * @return bool   true or false; 
  205.     */ 
  206.  function isEmpty(){ 
  207.   return !count($this->cart); 
  208.  }//開源代碼Vevb.com 
  209.  
  210.  // }}} 
  211.     // {{{ _stat() 
  212.  
  213.     /** 
  214.  * 取得部分統(tǒng)計信息 
  215.  * 
  216.     * @access private 
  217.     * @return bool  true or false; 
  218.     */ 
  219.  function _stat(){ 
  220.   if($this->isEmpty()) return false; 
  221.   foreach($this->cart as $item){ 
  222.    $this->totalCount   = @end($item); 
  223.    $this->totalPrices  = @prev($item); 
  224.   } 
  225.   return true; 
  226.  } 
  227.  
  228.  // }}} 
  229.     // {{{ totalPrices() 
  230.  
  231.     /** 
  232.  * 取得當前購物車所有商品的總金額 
  233.  * 
  234.     * @access public 
  235.     * @return float  返回金額; 
  236.     */ 
  237.  function totalPrices(){ 
  238.   if($this->_stat()) 
  239.    return $this->totalPrices; 
  240.  return 0; 
  241.  } 
  242.  
  243.  // }}} 
  244.     // {{{ isEmpty() 
  245.  
  246.     /** 
  247.  * 取得當前購物車所有商品的總數(shù)量和 
  248.  * 
  249.     * @access public 
  250.     * @return int ; 
  251.     */ 
  252.  function totalCount(){ 
  253.   if($this->_stat()) 
  254.    return $this->totalCount;   
  255.  return 0; 
  256.  } 
  257.  
  258.  
  259. }//End Class Cart 
  260. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 通城县| 锦州市| 班戈县| 防城港市| 衡东县| 昌邑市| 尉犁县| 衡南县| 区。| 南宫市| 宝鸡市| 南康市| 青铜峡市| 耒阳市| 财经| 康平县| 洛浦县| 晋中市| 电白县| 宁明县| 宜丰县| 夹江县| 武夷山市| 内江市| 奉新县| 满城县| 莫力| 镇康县| 思茅市| 兴城市| 宕昌县| 宜阳县| 白沙| 泰宁县| 晋州市| 桦南县| 海盐县| 日照市| 安徽省| 黔江区| 姚安县|