最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

实用的php购物车程序

时间:2014-09-06 14:28来源:未知 作者:最模板zuimoban 点击: 次
以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下,实例代码如下: ?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. } 
  8. $cart =& $_SESSION['cart']; 
  9.  
  10. if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='add') ){ 
  11.  $p = $_POST['p']; 
  12.  $items = $cart->add($p); 
  13. } 
  14. if( ($_GET['action']=='remove')&&($_GET['key']!="") ) { 
  15.  $items = $cart->remove($_GET['key']); 
  16. } 
  17.  
  18. if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='modi') ){ 
  19.  $key = $_POST['key']; 
  20.  $value = $_POST['value']; 
  21.  for($i=0;$i<count($key);$i  ){ 
  22.   $items = $cart->modi($key[$i],$value[$i]); 
  23.  } 
  24. } 
  25.  
  26. $items = $cart->getCart(); 
  27. //打印 
  28. echo "<table border=1>"; 
  29. setlocale(LC_MONETARY, 'it_IT'); 
  30. foreach($items as $item){ 
  31.  echo "<tr><form method="post" action="tmp.php">"; 
  32.  echo "<td>ID:".$item['ID']."<input type=hidden name=key[] value=".$item['ID'].">"; 
  33.  echo "<td>产品:".$item['name']; 
  34.  echo "<td>单价:".$item['price']; 
  35.  echo "<td><input type=text name=value[] value=".$item['count'].">"; 
  36.   $sum = $item['count']*$item['price']; 
  37.  echo "<td>合计:".round($sum,2); 
  38.  echo "<td><input type=button value='删除' onclick="location='?action=remove&key=".$item['ID']."'">"; 
  39. } 
  40. echo "<input type=hidden name=action value=modi>"; 
  41. echo "<tr><td colspan=7><input type=submit />"; 
  42. echo "</td></form></tr></table>"; 
  43.  
  44.  
  45. ?> 
  46. <hr> 
  47. <form method="post" action="tmp.php"> 
  48. ID:<input type="text" name="p[]" /> 
  49. 品名:<input type="text" name="p[]" /> 
  50. 单价:<input type="text" name="p[]" /> 
  51. 数量:<input type="text" name="p[]" /> 
  52. <input type=hidden name=action value=add> 
  53. <input type="submit" /> 
  54. </form> 
  55.  
  56.  
  57.  
  58. <? 
  59. /** 
  60.  * Cart 
  61.  *  
  62.  * 购物车类 
  63.  *  
  64.  * @author  doodoo<pWtitle@yahoo.com.cn> 
  65.  * @package     Cart 
  66.  * @category    Cart 
  67.  * @license     PHP License 
  68.  * @access      public 
  69.  * @version     $Revision: 1.10 $ 
  70.  */ 
  71. Class Cart{ 
  72.  
  73.  var $cart; 
  74.  var $totalCount; //商品总数量 
  75.  var $totalPrices; //商品总金额 
  76.  
  77.   /** 
  78.      * Cart Constructor 
  79.      *  
  80.      * 类的构造函数,使购物车保持稳定的初始化状态  
  81.      *  
  82.      * @static  
  83.      * @access  public  
  84.      * @return  void   无返回值 
  85.      * @param   void   无参数 
  86.      */ 
  87.   function Cart(){ 
  88.   $this->totalCount = 0; 
  89.   $this->totalPrice = 0; 
  90.   $this->cart = array(); 
  91.  } 
  92.  
  93.  // }}} 
  94.     // {{{ add($item) 
  95.  
  96.     /** 
  97.  * 增加商品到当前购物车 
  98.  * 
  99.     * @access public 
  100.     * @param  array $item 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量)) 
  101.     * @return array   返回当前购物车内商品的数组 
  102.     */ 
  103.  function add($item){ 
  104.   if(!is_array($item)||is_null($item)) return $this->cart; 
  105.   if(!is_numeric(end($item))||(!is_numeric(prev($item)))) { 
  106.    echo "价格和数量必须是数字"; 
  107.    return $this->cart; 
  108.   } 
  109.   reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标 
  110.   $key = current($item); 
  111.   if($key=="") return $this->cart; 
  112.   if($this->_isExists($key)){  //商品是否已经存在? 
  113.     $this->cart[$key]['count']  = end($item); 
  114.   return $this->cart; 
  115.   } 
  116.  
  117.   $this->cart[$key]['ID']  = $key; 
  118.   $this->cart[$key]['name'] = next($item); 
  119.   $this->cart[$key]['price'] = next($item); 
  120.   $this->cart[$key]['count'] = next($item); 
  121.  
  122.  return $this->cart; 
  123.  } 
  124.  
  125.  // }}} 
  126.     // {{{ add($item) 
  127.  
  128.     /** 
  129.  * 从当前购物车中取出部分或全部商品 
  130.  * 当 $key=="" 的时候,清空当前购物车 
  131.  * 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品  
  132.  * 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品  
  133.  * 
  134.     * @access public 
  135.     * @param  string $key 商品ID 
  136.     * @return mixed   返回真假或当前购物车内商品的数组 
  137.     */ 
  138.  function remove($key="",$count=""){ 
  139.   if($key=="") { 
  140.    $this->cart = array(); 
  141.    return true; 
  142.   } 
  143.   if(!array_key_exists($key,$this->cart)) return false; 
  144.   if($count==""){ //移去这一类商品 
  145.    unset($this->cart[$key]); 
  146.   }else{ //移去$count个商品 
  147.    $this->cart[$key]['count'] -= $count; 
  148.    if($this->cart[$key]['count']<=0) unset($this->cart[$key]); 
  149.   } 
  150.   return $this->cart; 
  151.  } 
  152.  
  153.  // }}} 
  154.     // {{{ modi($key,$value) 
  155.  
  156.     /** 
  157.  * 修改购物车内商品ID为 $key 的商品的数量为 $value 
  158.  * 
  159.     * @access public 
  160.     * @param  string $key 商品ID 
  161.     * @param  int $value 商品数量 
  162.     * @return array  返回当前购物车内商品的数组; 
  163.     */ 
  164.  function modi($key,$value){ 
  165.   if(!$this->_isExists($key)) return $this->cart();  //不存在此商品,直接返回 
  166.   if($value<=0){     // value 太小,全部删除 
  167.    unset($this->cart[$key]); 
  168.    return $this->cart; 
  169.   } 
  170.   $this->cart[$key]['count'] = $value; 
  171.   return $this->cart; 
  172.  } 
  173.  
  174.  
  175.     /** 
  176.  * 返回当前购物车内商品的数组 
  177.  * 
  178.     * @access public 
  179.     * @return array  返回当前购物车内商品的数组; 
  180.     */ 
  181.  function getCart(){ 
  182.   return $this->cart; 
  183.  } 
  184.  
  185.  // }}} 
  186.     // {{{ _isExists($key) 
  187.  
  188.     /** 
  189.  * 判断当前购物车中是否存在商品ID号为$key的商品 
  190.  * 
  191.     * @access private 
  192.     * @param  string $key 商品ID 
  193.     * @return bool   true or false; 
  194.     */ 
  195.     function _isExists($key) 
  196.     { 
  197.   if(isset($this->cart[$key])&&!emptyempty($this->cart[$key])&&array_key_exists($key,$this->cart)) 
  198.    return true; 
  199.     return false; 
  200.     } 
  201.  
  202.  // }}} 
  203.     // {{{ isEmpty() 
  204.  
  205.     /** 
  206.  * 判断当前购物车是否为空,即没有任何商品 
  207.  * 
  208.     * @access public 
  209.     * @return bool   true or false; 
  210.     */ 
  211.  function isEmpty(){ 
  212.   return !count($this->cart); 
  213.  }//开源代码vcphp.com 
  214.  
  215.  // }}} 
  216.     // {{{ _stat() 
  217.  
  218.     /** 
  219.  * 取得部分统计信息 
  220.  * 
  221.     * @access private 
  222.     * @return bool  true or false; 
  223.     */ 
  224.  function _stat(){ 
  225.   if($this->isEmpty()) return false; 
  226.   foreach($this->cart as $item){ 
  227.    $this->totalCount   = @end($item); 
  228.    $this->totalPrices  = @prev($item); 
  229.   } 
  230.   return true; 
  231.  } 
  232.  
  233.  // }}} 
  234.     // {{{ totalPrices() 
  235.  
  236.     /** 
  237.  * 取得当前购物车所有商品的总金额 
  238.  * 
  239.     * @access public 
  240.     * @return float  返回金额; 
  241.     */ 
  242.  function totalPrices(){ 
  243.   if($this->_stat()) 
  244.    return $this->totalPrices; 
  245.  return 0; 
  246.  } 
  247.  
  248.  // }}} 
  249.     // {{{ isEmpty() 
  250.  
  251.     /** 
  252.  * 取得当前购物车所有商品的总数量和 
  253.  * 
  254.     * @access public 
  255.     * @return int ; 
  256.     */ 
  257.  function totalCount(){ 
  258.   if($this->_stat()) 
  259.    return $this->totalCount;   
  260.  return 0; 
  261.  } 
  262.  
  263.  
  264. }//End Class Cart 
  265. ?> 
  266.  
(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容