服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

实用的php购物车程序

时间:2016-05-08 10:37来源: 作者: 点击:
以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下,实例代码如下: ?php //调用实例 require_once cart.class.php ; session_start(); if (!isset( $_SESSION [ cart ])){ $_SESSIO

以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下,实例代码如下:

  1. <?php 
  2. //调用实例 
  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>产品:".$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. 数量:<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//商品总数量 
  70.  var $totalPrices//商品总金额 
  71.  
  72.   /** 
  73.      * Cart Constructor 
  74.      *  
  75.      * 类的构造函数,使购物车保持稳定的初始化状态  
  76.      *  
  77.      * @static  
  78.      * @access  public  
  79.      * @return  void   无返回值 
  80.      * @param   void   无参数 
  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 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量)) 
  96.     * @return array   返回当前购物车内商品的数组 
  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 "价格和数量必须是数字"
  102.    return $this->cart; 
  103.   } 
  104.   reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标 
  105.   $key = current($item); 
  106.   if($key==""return $this->cart; 
  107.   if($this->_isExists($key)){  //商品是否已经存在? 
  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   返回真假或当前购物车内商品的数组 
  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.  * 修改购物车内商品ID为 $key 的商品的数量为 $value 
  153.  * 
  154.     * @access public 
  155.     * @param  string $key 商品ID 
  156.     * @param  int $value 商品数量 
  157.     * @return array  返回当前购物车内商品的数组; 
  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.  * 返回当前购物车内商品的数组 
  172.  * 
  173.     * @access public 
  174.     * @return array  返回当前购物车内商品的数组; 
  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.  }//开源代码最模板zuimoban.com 
  209.  
  210.  // }}} 
  211.     // {{{ _stat() 
  212.  
  213.     /** 
  214.  * 取得部分统计信息 
  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.  * 取得当前购物车所有商品的总数量和 
  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. ?> 
  261.  
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容