本文以实例介绍使用PHP实现购物车的方法。 分享给大家,供大家参考。 具体分析如下:
php购物车用于电子商务网站。 它就像一个超市购物车。 选好商品后放入自己的购物车,在柜台等待结算。 这个php购物车是完全按照这个原则来举例的。 感兴趣的朋友可以看看。 这个例子就是用它来实现的。 代码如下:
代码如下:
班级 {
$ = 数组(); // 存放购物车的二维数组
$; //统计购物车的数量
$ = 86400; // 过期时间,如果为0,则不会以秒为单位保存到本地
($id = "", $name = "", $ = "", $ = "", $ = "", $count = "", $image = "", $ = 86400) {
如果 ($id != "" && ($id)) {
$this->=$;
$this->($id,$name,$,$,$,$count,$image);
}
}
($id, $name, $, $, $, $count, $image) {
$this-> = $this->(); // 读写数据到数组
if ($this->($id)) { // 检查产品是否存在
$this->($id,$count,0); // 产品数量加上$count
错误的;
}
$this->[0][$id] = $id;
$this->[1][$id] = $name;
$this->[2][$id] = $;
$this->[3][$id] = $;
$this->[4][$id] = $;
$this->[5][$id] = $count;
$this->[6][$id] = $image;
$this->保存();
}
($id, $count, $flag = "") {
$tmpid = $id;
$this-> = $this->(); // 读写数据到数组
$ = &$这个->; // 引用
如果 (!($[0])) 假;
如果($id < 1){
错误的;
}
($[0] 作为 $item) {
如果($item === $tmpid){
($旗帜) {
case 0: // 添加数量一般$count为1
$[5][$id] += $count;
休息;
案例 1:// 减少数量
$[5][$id] -= $count;
休息;
case 2: // 修改数量
如果($count == 0){
取消设置($[0][$id]);
取消设置($[1][$id]);
取消设置($[2][$id]);
取消设置($[3][$id]);
取消设置($[4][$id]);
取消设置($[5][$id]);
取消设置($[6][$id]);
休息;
} 别的 {
$[5][$id] = $计数;
休息;
}
案例 3:// 清除项目
取消设置($[0][$id]);
取消设置($[1][$id]);
取消设置($[2][$id]);
取消设置($[3][$id]);
取消设置($[4][$id]);
取消设置($[5][$id]);
取消设置($[6][$id]);
休息;
:
休息;
}
}
}
$this->保存();
}
() {
$this->= array();
$this->保存();
}
() {
$ = 教程($['']);
如果(!$)假;
$ = ($);
$;
}
() {
$ = $this->();
如果(计数($[0])< 1){
错误的;
}
真的;
}
() {
$ = $this-> = $this->();
$ = 数组(); // 一维数组
// 0是产品1的总价
// 1是产品2的总价
// 2 是产品 3 的总价
// 3是产品的总数量
$i = 0;
如果(($[0])){
($[0] 作为 $key=>$val) {
$[0] += $[2][$key] * $[5][$key];
$[1] += $[3][$key] * $[5][$key];
$[2] += $[4][$key] * $[5][$key];
$[3] += $[5][$键];
$i++;
}
}
$;
}
() {
$ = $this->();
$ = 计数($[0]);
$this->=$;
$;
}
节省() {
$ = $this->;
$ = ($);
("",$,time()+$this->);
}
($id) {
$ = $this->;
如果 (!($[0])) ;
($[0] 作为 $item) {
如果($item === $id)为真;
}
错误的;
}
}
希望本文对您的 PHP 编程有所帮助。