课课家小编最近涉及到一个项目相关的商品店,给正在PHP学习的教程概述。的这个很明显,购物车必须有,我记得很久以前用PHP的购物车做了,但没有保存,之前不能犯这样的错误,通过各种研究,我也总结出一个购物车PHP实现类。加入实现商品的购物车,修改,删除,列表和各种计算的相关功能。使用单一类型的PHP,安全,高效,简便,易扩展的原则。

005 |
class Cart{
|
006 |
static protected $ins; //实例变量
|
007 |
public $item=array(); //放商品容器
|
008 |
//禁止外部调用
|
009 |
final protected function __construct(){}
|
010 |
//禁止克隆
|
011 |
final protected function __clone(){}
|
012 |
//类内部实例化
|
013 |
static protected function getIns(){
|
014 |
if(!(self::$ins instanceof self)){self::$ins=new self();}return self::$ins;
|
015 |
}
|
016 |
//为了能使商品跨页面保存,把对象放入session里,这里为了防止冲突,设置了一个session后缀参数
|
017 |
public static function getCat($sesSuffix='phpernote'){
|
018 |
if(!isset($_SESSION['cat'.$sesSuffix])||!($_SESSION['cat'.$sesSuffix] instanceof self)){
|
019 |
$_SESSION['cat'.$sesSuffix]=self::getIns();
|
020 |
}
|
021 |
return $_SESSION['cat'.$sesSuffix];
|
022 |
}
|
023 |
//入列时的检验,是否在$item里存在
|
024 |
public function inItem($goods_id){
|
025 |
if($this->getTypes()==0){
|
026 |
return false;
|
027 |
}
|
028 |
//这里检验商品是否相同是通过goods_id来检测,并未通过商品名称name来检测,具体情况可做修改
|
029 |
if(!(array_key_exists($goods_id,$this->item))){
|
030 |
return false;
|
031 |
}else{
|
032 |
return $this->item[$goods_id]['num']; //返回此商品个数
|
033 |
}
|
034 |
}
|
035 |
//添加一个商品
|
036 |
/*
|
037 |
* goods_id 唯一id
|
038 |
* name 名称
|
039 |
* num 数量
|
040 |
* price 单价
|
041 |
*/
|
042 |
public function addItem($goods_id,$name,$num,$price){
|
043 |
if($this->inItem($goods_id)!=false){
|
044 |
$this->item[$goods_id]['num']+=$num;
|
045 |
return;
|
046 |
}
|
047 |
$this->item[$goods_id]=array(); //一个商品为一个数组
|
048 |
$this->item[$goods_id]['num']=$num; //这一个商品的购买数量
|
049 |
$this->item[$goods_id]['name']=$name; //商品名字
|
050 |
$this->item[$goods_id]['price']=floatval($price); //商品单价
|
051 |
}
|
052 |
//减少一个商品
|
053 |
public function reduceItem($goods_id,$num){
|
054 |
if($this->inItem($goods_id)==false){
|
055 |
return;
|
056 |
}
|
057 |
if($num>$this->getNum($goods_id)){
|
058 |
unset($this->item[$goods_id]);
|
059 |
}else{
|
060 |
$this->item[$goods_id]['num']-=$num;
|
061 |
}
|
062 |
}
|
063 |
//去掉一个商品
|
064 |
public function delItem($goods_id){
|
065 |
if($this->inItem($goods_id)){
|
066 |
unset($this->item[$goods_id]);
|
067 |
}
|
068 |
}
|
069 |
//返回购买商品列表
|
070 |
public function itemList(){
|
071 |
return $this->item;
|
072 |
}
|
073 |
//一共有多少种商品
|
074 |
public function getTypes(){
|
075 |
return count($this->item);
|
076 |
}
|
077 |
//获得一种商品的总个数
|
078 |
public function getNum($goods_id){
|
079 |
return $this->item[$goods_id]['num'];
|
080 |
}
|
081 |
// 查询购物车中有多少个商品
|
082 |
public function getNumber(){
|
083 |
$num=0;
|
084 |
if($this->getTypes()==0){
|
085 |
return 0;
|
086 |
}
|
087 |
foreach($this->item as $k=>$v){
|
088 |
$num+=$v['num'];
|
089 |
}
|
090 |
return $num;
|
091 |
}
|
092 |
//计算总价格
|
093 |
public function getPrice(){
|
094 |
$price=0;
|
095 |
if($this->getTypes()==0){
|
096 |
return 0;
|
097 |
}
|
098 |
foreach($this->item as $k=>$v){
|
099 |
$price+=floatval($v['num']*$v['price']);
|
100 |
}
|
101 |
return $price;
|
102 |
}
|
103 |
//清空购物车
|
104 |
public function emptyItem(){
|
105 |
$this->item=array();
|
106 |
}
|
107 |
} |
#p#分页标题#e#
以上购物车类的调用示例如下:
01 |
|
02 |
header("Content-type:text/html;charset=utf-8");
|
03 |
session_start(); |
04 |
$cart = Cart::getCat('_test');
|
05 |
//cart经过一次实例化之后,任意页面都可以通过$_SESSION['cat_test']调用cart类的相关方法 |
06 |
$_SESSION['cat_test']->addItem('1','苹果','1','8.03');
|
07 |
$cart->addItem('2','香蕉','3','6.5');
|
08 |
echo '
|
09 |
print_r($_SESSION);
|
10 |
echo '获取购物车商品名称:'.$_SESSION['cat_test']->item[1]['name'],';',$_SESSION['cat_test']->item[2]['name'];
|
11 |
echo ';
|
12 |
echo '购物车中共有商品总数:',$cart->getNumber();
|
13 |
echo ';
|
14 |
echo '购物车中商品总价:',$_SESSION['cat_test']->getPrice();
|
15 |
//session_destroy(); |
16 |
//$_SESSION['cat_test']->emptyItem(); |
最后php学习掌握以上方法完全可以实现商品的购物车啦!猜您喜欢PHP视频教程的课程:| 站长聚集地php教程 |