php 购物车是在电子商务网站会用到的,一种像超市购物车一样的,选好商品了,先放到自己的购物车里面等好了再到柜台结算,本款php购物车完全按照这个原理来实例的,下面我们来看看吧,利用了cookie来实现,代码如下:
- <?php
-
-
-
-
- class cartapi {
- private $cartarray = array();
- private $cartcount;
- public $expires = 86400;
-
-
-
-
- public function __construct($id = "",$name = "",$price1 = "",$price2 = "",$price3 = "",$count = "",$image = "",$expires = 86400) {
- if ($id != "" && is_numeric($id)) {
- $this->expires = $expires;
- $this->addcart($id,$name,$price1,$price2,$price3,$count,$image);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public function addcart($id,$name,$price1,$price2,$price3,$count,$image) {
- $this->cartarray = $this->cartview();
- if ($this->checkitem($id)) {
- $this->modifycart($id,$count,0);
- return false;
- }
- $this->cartarray[0][$id] = $id;
- $this->cartarray[1][$id] = $name;
- $this->cartarray[2][$id] = $price1;
- $this->cartarray[3][$id] = $price2;
- $this->cartarray[4][$id] = $price3;
- $this->cartarray[5][$id] = $count;
- $this->cartarray[6][$id] = $image;
- $this->save();
- }
-
-
-
-
-
-
-
-
- public function modifycart($id, $count, $flag = "") {
- $tmpid = $id;
- $this->cartarray = $this->cartview();
- $tmparray = &$this->cartarray;
- if (!is_array($tmparray[0])) return false;
- if ($id < 1) {
- return false;
- }
- foreach ($tmparray[0] as $item) {
- if ($item === $tmpid) {
- switch ($flag) {
- case 0:
- $tmparray[5][$id] += $count;
- break;
- case 1:
- $tmparray[5][$id] -= $count;
- break;
- case 2:
- if ($count == 0) {
- unset($tmparray[0][$id]);
- unset($tmparray[1][$id]);
- unset($tmparray[2][$id]);
- unset($tmparray[3][$id]);
- unset($tmparray[4][$id]);
- unset($tmparray[5][$id]);
- unset($tmparray[6][$id]);
- break;
- } else {
- $tmparray[5][$id] = $count;
- break;
- }
- case 3:
- unset($tmparray[0][$id]);
- unset($tmparray[1][$id]);
- unset($tmparray[2][$id]);
- unset($tmparray[3][$id]);
- unset($tmparray[4][$id]);
- unset($tmparray[5][$id]);
- unset($tmparray[6][$id]);
- break;
- default:
- break;
- }
- }
- }
- $this->save();
- }
-
-
-
-
- public function removeall() {
- $this->cartarray = array();
- $this->save();
- }
-
-
-
-
-
- public function cartview() {
- $cookie = strips教程lashes($_cookie['cartapi']);
- if (!$cookie) return false;
- $tmpunserialize = unserialize($cookie);
- return $tmpunserialize;
- }
-
-
-
-
-
- public function checkcart() {
- $tmparray = $this->cartview();
- if (count($tmparray[0]) < 1) {
- return false;
- }
- return true;
- }
-
-
-
-
-
- public function countprice() {
- $tmparray = $this->cartarray = $this->cartview();
- $outarray = array();
-
-
-
-
- $i = 0;
- if (is_array($tmparray[0])) {
- foreach ($tmparray[0] as $key=>$val) {
- $outarray[0] += $tmparray[2][$key] * $tmparray[5][$key];
- $outarray[1] += $tmparray[3][$key] * $tmparray[5][$key];
- $outarray[2] += $tmparray[4][$key] * $tmparray[5][$key];
- $outarray[3] += $tmparray[5][$key];
- $i++;
- }
- }
- return $outarray;
- }
-
-
-
-
-
- public function cartcount() {
- $tmparray = $this->cartview();
- $tmpcount = count($tmparray[0]);
- $this->cartcount = $tmpcount;
- return $tmpcount;
- }
-
-
-
-
- public function save() {
- $tmparray = $this->cartarray;
- $tmpserialize = serialize($tmparray);
- setcookie("cartapi",$tmpserialize,time()+$this->expires);
- }
-
-
-
-
-
-
- private function checkitem($id) {
-
- $tmparray = $this->cartarray;
- if (!is_array($tmparray[0])) return;
- foreach ($tmparray[0] as $item) {
- if ($item === $id) return true;
- }
- return false;
- }
- }
- ?>
|