设为首页 收藏本站
查看: 1291|回复: 0

[经验分享] [share]PHP购物车类的源码

[复制链接]

尚未签到

发表于 2015-8-23 12:41:48 | 显示全部楼层 |阅读模式
  



<?php
//调用实例
require_once 'cart.class.php';
session_start();
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = new Cart;
}
$cart =& $_SESSION['cart'];
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='add') ){
$p = $_POST['p'];
$items = $cart->add($p);
}
if( ($_GET['action']=='remove')&&($_GET['key']!="") ) {
$items = $cart->remove($_GET['key']);
}
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='modi') ){
$key = $_POST['key'];
$value = $_POST['value'];
for($i=0;$i<count($key);$i  ){
  $items = $cart->modi($key[$i],$value[$i]);
}
}
$items = $cart->getCart();
//打印
echo "<table border=1>";
setlocale(LC_MONETARY, 'it_IT');
foreach($items as $item){
echo "<tr><form method=\"post\" action=\"tmp.php\">";
echo "<td>ID:".$item['ID']."<input type=hidden name=key[] value=".$item['ID'].">";
echo "<td>产品:".$item['name'];
echo "<td>单价:".$item['price'];
echo "<td><input type=text name=value[] value=".$item['count'].">";
  $sum = $item['count']*$item['price'];
echo "<td>合计:".round($sum,2);
echo "<td><input type=button value='删除' onclick=\"location='?action=remove&key=".$item['ID']."'\">";
}
echo "<input type=hidden name=action value=modi>";
echo "<tr><td colspan=7><input type=submit />";
echo "</td></form></tr></table>";

?>
<hr>
<form method="post" action="tmp.php">
ID:<input type="text" name="p[]" />
品名:<input type="text" name="p[]" />
单价:<input type="text" name="p[]" />
数量:<input type="text" name="p[]" />
<input type=hidden name=action value=add>
<input type="submit" />
</form>
  



<?
/**
* Cart
*
* 购物车类
*
* @author  doodoo<pWtitle@yahoo.com.cn>
* @package     Cart
* @category    Cart
* @license     PHP License
* @access      public
* @version     $Revision: 1.10 $
*/
Class Cart{
var $cart;
var $totalCount; //商品总数量
var $totalPrices; //商品总金额

  /**
     * Cart Constructor
     *
     * 类的构造函数,使购物车保持稳定的初始化状态
     *
     * @static
     * @access  public
     * @return  void   无返回值
     * @param   void   无参数
     */
  function Cart(){
  $this->totalCount = 0;
  $this->totalPrice = 0;
  $this->cart = array();
}
// }}}
    // {{{ add($item)

    /**
* 增加商品到当前购物车
*
    * @access public
    * @param  array $item 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量))
    * @return array   返回当前购物车内商品的数组
    */
function add($item){
  if(!is_array($item)||is_null($item)) return $this->cart;
  if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
   echo "价格和数量必须是数字";
   return $this->cart;
  }
  reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标
  $key = current($item);
  if($key=="") return $this->cart;
  if($this->_isExists($key)){  //商品是否已经存在?
    $this->cart[$key]['count']  = end($item);
  return $this->cart;
  }
  $this->cart[$key]['ID']  = $key;
  $this->cart[$key]['name'] = next($item);
  $this->cart[$key]['price'] = next($item);
  $this->cart[$key]['count'] = next($item);
return $this->cart;
}
// }}}
    // {{{ add($item)

    /**
* 从当前购物车中取出部分或全部商品
* 当 $key=="" 的时候,清空当前购物车
* 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品
* 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品
*
    * @access public
    * @param  string $key 商品ID
    * @return mixed   返回真假或当前购物车内商品的数组
    */
function remove($key="",$count=""){
  if($key=="") {
   $this->cart = array();
   return true;
  }
  if(!array_key_exists($key,$this->cart)) return false;
  if($count==""){ //移去这一类商品
   unset($this->cart[$key]);
  }else{ //移去$count个商品
   $this->cart[$key]['count'] -= $count;
   if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
  }
  return $this->cart;
}
// }}}
    // {{{ modi($key,$value)

    /**
* 修改购物车内商品ID为 $key 的商品的数量为 $value
*
    * @access public
    * @param  string $key 商品ID
    * @param  int $value 商品数量
    * @return array  返回当前购物车内商品的数组;
    */
function modi($key,$value){
  if(!$this->_isExists($key)) return $this->cart();  //不存在此商品,直接返回
  if($value<=0){     // value 太小,全部删除
   unset($this->cart[$key]);
   return $this->cart;
  }
  $this->cart[$key]['count'] = $value;
  return $this->cart;
}

    /**
* 返回当前购物车内商品的数组
*
    * @access public
    * @return array  返回当前购物车内商品的数组;
    */
function getCart(){
  return $this->cart;
}
// }}}
    // {{{ _isExists($key)

    /**
* 判断当前购物车中是否存在商品ID号为$key的商品
*
    * @access private
    * @param  string $key 商品ID
    * @return bool   true or false;
    */
    function _isExists($key)
    {
  if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart))
   return true;
    return false;
    }
// }}}
    // {{{ isEmpty()

    /**
* 判断当前购物车是否为空,即没有任何商品
*
    * @access public
    * @return bool   true or false;
    */
function isEmpty(){
  return !count($this->cart);
}
// }}}
    // {{{ _stat()

    /**
* 取得部分统计信息
*
    * @access private
    * @return bool  true or false;
    */
function _stat(){
  if($this->isEmpty()) return false;
  foreach($this->cart as $item){
   $this->totalCount   = @end($item);
   $this->totalPrices  = @prev($item);
  }
  return true;
}
// }}}
    // {{{ totalPrices()

    /**
* 取得当前购物车所有商品的总金额
*
    * @access public
    * @return float  返回金额;
    */
function totalPrices(){
  if($this->_stat())
   return $this->totalPrices;
return 0;
}
// }}}
    // {{{ isEmpty()

    /**
* 取得当前购物车所有商品的总数量和
*
    * @access public
    * @return int ;
    */
function totalCount(){
  if($this->_stat())
   return $this->totalCount;  
return 0;
}

}//End Class Cart
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-102972-1-1.html 上篇帖子: 从零开始PHP学习 下篇帖子: PHP源代码数组统计count分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表