亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php利用cookies實現(xiàn)購物車的方法

 更新時間:2014年12月10日 10:24:24   投稿:shichen2014  
這篇文章主要介紹了php利用cookies實現(xiàn)購物車的方法,可通過cookie實現(xiàn)對商品的增刪改等功能,以及統(tǒng)計與檢查等技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php利用cookies實現(xiàn)購物車的方法。分享給大家供大家參考。具體分析如下:

php購物車是在電子商務(wù)網(wǎng)站會用到的,一種像超市購物車一樣的,選好商品了,先放到自己的購物車?yán)锩娴群昧嗽俚焦衽_結(jié)算,本款php購物車完全按照這個原理來實例的,感興趣的朋友可以來看看,該實例利用了cookie來實現(xiàn),代碼如下:

復(fù)制代碼 代碼如下:
<?php
/**
 * 購物車類 cookies 保存,保存周期為1天 注意:瀏覽器必須支持cookie才能夠使用
 */
class cartapi {
 private $cartarray = array(); // 存放購物車的二維數(shù)組
 private $cartcount; // 統(tǒng)計購物車數(shù)量
 public $expires = 86400; // cookies過期時間,如果為0則不保存到本地 單位為秒
 /**
  * 構(gòu)造函數(shù) 初始化操作 如果$id不為空,則直接添加到購物車
  *
  */
 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);
  }
 }
 /**
  * 添加商品到購物車
  *
  * @param int $id 商品的編號
  * @param string $name 商品名稱
  * @param decimal $price1 商品價格
  * @param decimal $price2 商品價格
  * @param decimal $price3 商品價格
  * @param int $count 商品數(shù)量
  * @param string $image 商品圖片
  * @return 如果商品存在,則在原來的數(shù)量上加1,并返回false
  */
 public function addcart($id,$name,$price1,$price2,$price3,$count,$image) {
  $this->cartarray = $this->cartview(); // 把數(shù)據(jù)讀取并寫入數(shù)組
  if ($this->checkitem($id)) { // 檢測商品是否存在
   $this->modifycart($id,$count,0); // 商品數(shù)量加$count
   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();
 }
 /**
  * 修改購物車?yán)锏纳唐?
  *
  * @param int $id 商品編號
  * @param int $count 商品數(shù)量
  * @param int $flag 修改類型 0:加 1:減 2:修改 3:清空
  * @return 如果修改失敗,則返回false
  */
 public function modifycart($id, $count, $flag = "") {
  $tmpid = $id;
  $this->cartarray = $this->cartview(); // 把數(shù)據(jù)讀取并寫入數(shù)組
  $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: // 添加數(shù)量 一般$count為1
      $tmparray[5][$id] += $count;
      break;
     case 1: // 減少數(shù)量
      $tmparray[5][$id] -= $count;
      break;
     case 2: // 修改數(shù)量
      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();
 }
 /**
  * 查看購物車信息
  *
  * @return array 返回一個二維數(shù)組
  */
 public function cartview() {
  $cookie = strips教程lashes($_cookie['cartapi']);
  if (!$cookie) return false;
  $tmpunserialize = unserialize($cookie);
  return $tmpunserialize;
 }
 /**
  * 檢查購物車是否有商品
  *
  * @return bool 如果有商品,返回true,否則false
  */
 public function checkcart() {
  $tmparray = $this->cartview();
  if (count($tmparray[0]) < 1) {   
   return false;
  }
  return true;
 }
 /**
  * 商品統(tǒng)計
  *
  * @return array 返回一個一維數(shù)組 $arr[0]:產(chǎn)品1的總價格 $arr[1:產(chǎn)品2得總價格 $arr[2]:產(chǎn)品3的總價格 $arr[3]:產(chǎn)品的總數(shù)量
  */
 public function countprice() {
  $tmparray = $this->cartarray = $this->cartview();
  $outarray = array(); //一維數(shù)組
  // 0 是產(chǎn)品1的總價格
  // 1 是產(chǎn)品2的總價格
  // 2 是產(chǎn)品3的總價格
  // 3 是產(chǎn)品的總數(shù)量
  $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;
 }
 /**
  * 統(tǒng)計商品數(shù)量
  *
  * @return int
  */
 public function cartcount() {
  $tmparray = $this->cartview();
  $tmpcount = count($tmparray[0]);
  $this->cartcount = $tmpcount;
  return $tmpcount;
 }
 /**
  * 保存商品 如果不使用構(gòu)造方法,此方法必須使用
  *
  */
 public function save() {
  $tmparray = $this->cartarray;
  $tmpserialize = serialize($tmparray);
  setcookie("cartapi",$tmpserialize,time()+$this->expires);
 }
 /**
  * 檢查購物車商品是否存在
  *
  * @param int $id
  * @return bool 如果存在 true 否則false
  */
 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;
 }
}
?>

希望本文所述對大家的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論