欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php利用cookies實(shí)現(xiàn)購(gòu)物車的方法

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

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

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

復(fù)制代碼 代碼如下:
<?php
/**
 * 購(gòu)物車類 cookies 保存,保存周期為1天 注意:瀏覽器必須支持cookie才能夠使用
 */
class cartapi {
 private $cartarray = array(); // 存放購(gòu)物車的二維數(shù)組
 private $cartcount; // 統(tǒng)計(jì)購(gòu)物車數(shù)量
 public $expires = 86400; // cookies過期時(shí)間,如果為0則不保存到本地 單位為秒
 /**
  * 構(gòu)造函數(shù) 初始化操作 如果$id不為空,則直接添加到購(gòu)物車
  *
  */
 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);
  }
 }
 /**
  * 添加商品到購(gòu)物車
  *
  * @param int $id 商品的編號(hào)
  * @param string $name 商品名稱
  * @param decimal $price1 商品價(jià)格
  * @param decimal $price2 商品價(jià)格
  * @param decimal $price3 商品價(jià)格
  * @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)) { // 檢測(cè)商品是否存在
   $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();
 }
 /**
  * 修改購(gòu)物車?yán)锏纳唐?
  *
  * @param int $id 商品編號(hào)
  * @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();
 }
 /**
  * 清空購(gòu)物車
  *
  */
 public function removeall() {
  $this->cartarray = array();
  $this->save();
 }
 /**
  * 查看購(gòu)物車信息
  *
  * @return array 返回一個(gè)二維數(shù)組
  */
 public function cartview() {
  $cookie = strips教程lashes($_cookie['cartapi']);
  if (!$cookie) return false;
  $tmpunserialize = unserialize($cookie);
  return $tmpunserialize;
 }
 /**
  * 檢查購(gòu)物車是否有商品
  *
  * @return bool 如果有商品,返回true,否則false
  */
 public function checkcart() {
  $tmparray = $this->cartview();
  if (count($tmparray[0]) < 1) {   
   return false;
  }
  return true;
 }
 /**
  * 商品統(tǒng)計(jì)
  *
  * @return array 返回一個(gè)一維數(shù)組 $arr[0]:產(chǎn)品1的總價(jià)格 $arr[1:產(chǎn)品2得總價(jià)格 $arr[2]:產(chǎn)品3的總價(jià)格 $arr[3]:產(chǎn)品的總數(shù)量
  */
 public function countprice() {
  $tmparray = $this->cartarray = $this->cartview();
  $outarray = array(); //一維數(shù)組
  // 0 是產(chǎn)品1的總價(jià)格
  // 1 是產(chǎn)品2的總價(jià)格
  // 2 是產(chǎn)品3的總價(jià)格
  // 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)計(jì)商品數(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);
 }
 /**
  * 檢查購(gòu)物車商品是否存在
  *
  * @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;
 }
}
?>

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

相關(guān)文章

最新評(píng)論