国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 23:28:49
字體:
供稿:網(wǎng)友
這篇文章主要介紹了php利用cookies實(shí)現(xiàn)購物車的方法,可通過cookie實(shí)現(xiàn)對(duì)商品的增刪改等功能,以及統(tǒng)計(jì)與檢查等技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
 
 

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

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

復(fù)制代碼代碼如下:
<?php 
/** 
 * 購物車類 cookies 保存,保存周期為1天 注意:瀏覽器必須支持cookie才能夠使用 
 */ 
class cartapi { 
 private $cartarray = array(); // 存放購物車的二維數(shù)組 
 private $cartcount; // 統(tǒng)計(jì)購物車數(shù)量 
 public $expires = 86400; // cookies過期時(shí)間,如果為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 商品的編號(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(); 
 } 
 /** 
  * 修改購物車?yán)锏纳唐?nbsp;
  * 
  * @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(); 
 } 
 /** 
  * 清空購物車 
  * 
  */ 
 public function removeall() { 
  $this->cartarray = array(); 
  $this->save(); 
 } 
 /** 
  * 查看購物車信息 
  * 
  * @return array 返回一個(gè)二維數(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)計(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); 
 } 
 /** 
  * 檢查購物車商品是否存在 
  * 
  * @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ì)有所幫助。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 孝义市| 海林市| 怀集县| 荥阳市| 岑巩县| 亳州市| 巴东县| 化州市| 北辰区| 广平县| 金溪县| 华池县| 锦屏县| 山东| 德清县| 沁阳市| 斗六市| 朝阳市| 无为县| 芦溪县| 连江县| 克什克腾旗| 互助| 廊坊市| 象州县| 万载县| 黑龙江省| 杭锦后旗| 军事| 合江县| 拜泉县| 云阳县| 区。| 聂拉木县| 绥化市| 通河县| 新和县| 乐亭县| 玛纳斯县| 疏附县| 原阳县|