国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) 後端開發(fā) php教程 PHP購(gòu)物車種,移植于CodeIgniter

PHP購(gòu)物車種,移植于CodeIgniter

Jun 13, 2016 am 10:39 AM
items return this

PHP購(gòu)物車類,移植于CodeIgniter

<?php /** * 購(gòu)物車程序 Modified by CodeIgniter * */class cart {	// 對(duì)產(chǎn)品ID和產(chǎn)品名稱進(jìn)行正則驗(yàn)證屬性	var $product_id_rules	= '\.a-z0-9_-';	var $product_name_rules	= '\.\:\-_ a-z0-9'; // 考慮到漢字,該功能暫不使用	// 私有變量	var $_cart_contents	= array();	/**	 * 構(gòu)造方法	 *	 */	public function __construct()	{		if ($this->session('cart_contents') !== FALSE)		{			$this->_cart_contents = $this->session('cart_contents');		}		else		{			// 初始化數(shù)據(jù)			$this->_cart_contents['cart_total'] = 0;			$this->_cart_contents['total_items'] = 0;		}	}	// --------------------------------	/**	 * 添加到購(gòu)物車	 *	 * @access	public	 * @param	array	 * @return	bool	 */	function insert($items = array())	{		// 檢測(cè)數(shù)據(jù)是否正確		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		// 可以添加一個(gè)商品(一維數(shù)組),也可以添加多個(gè)商品(二維數(shù)組)		$save_cart = FALSE;		if (isset($items['id']))		{			if ($this->_insert($items) == TRUE)			{				$save_cart = TRUE;			}		}		else		{			foreach ($items as $val)			{				if (is_array($val) AND isset($val['id']))				{					if ($this->_insert($val) == TRUE)					{						$save_cart = TRUE;					}				}			}		}		// 更新數(shù)據(jù)		if ($save_cart == TRUE)		{			$this->_save_cart();			return TRUE;		}		return FALSE;	}	// --------------------------------	/**	 * 處理插入購(gòu)物車數(shù)據(jù)	 *	 * @access	private	 * @param	array	 * @return	bool	 */	function _insert($items = array())	{		// 檢查購(gòu)物車		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		// --------------------------------		/* 前四個(gè)數(shù)組索引 (id, qty, price 和name) 是 必需的。		   如果缺少其中的任何一個(gè),數(shù)據(jù)將不會(huì)被保存到購(gòu)物車中。		   第5個(gè)索引 (options) 是可選的。當(dāng)你的商品包含一些相關(guān)的選項(xiàng)信息時(shí),你就可以使用它。		   請(qǐng)使用一個(gè)數(shù)組來(lái)保存選項(xiàng)信息。注意:$data['price'] 的值必須大于0 		   如:$data = array(               'id'      => 'sku_123ABC',               'qty'     => 1,               'price'   => 39.95,               'name'    => 'T-Shirt',               'options' => array('Size' => 'L', 'Color' => 'Red')            );		*/		if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))		{			return FALSE;		}		// --------------------------------		// 數(shù)量驗(yàn)證,不是數(shù)字替換為空		$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));		// 數(shù)量驗(yàn)證		$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));		// 數(shù)量必須是數(shù)字或不為0		if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)		{			return FALSE;		}		// --------------------------------		// 產(chǎn)品ID驗(yàn)證		if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))		{			return FALSE;		}		// --------------------------------		// 驗(yàn)證產(chǎn)品名稱,考慮到漢字,暫不使用		/*		if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))		{			return FALSE;		}		*/		// --------------------------------		// 價(jià)格驗(yàn)證		$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));		$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));		// 驗(yàn)證價(jià)格是否是數(shù)值		if ( ! is_numeric($items['price']))		{			return FALSE;		}		// --------------------------------		// 屬性驗(yàn)證,如果屬性存在,屬性值+產(chǎn)品ID進(jìn)行加密保存在$rowid中		if (isset($items['options']) AND count($items['options']) > 0)		{			$rowid = md5($items['id'].implode('', $items['options']));		}		else		{			// 沒(méi)有屬性時(shí)直接對(duì)產(chǎn)品ID加密			$rowid = md5($items['id']);		}				// 檢測(cè)購(gòu)物車中是否有該產(chǎn)品,如果有,在原來(lái)的基礎(chǔ)上加上本次新增的商品數(shù)量		$_contents = $this->_cart_contents;		$_tmp_contents = array();		foreach ($_contents as $val)		{			if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']) AND $val['rowid']==$rowid)			{				$_tmp_contents[$val['rowid']]['qty'] = $val['qty'];			} else {				$_tmp_contents[$val['rowid']]['qty'] = 0;			}		}		// --------------------------------		// 清除原來(lái)的數(shù)據(jù)		unset($this->_cart_contents[$rowid]);		// 重新賦值		$this->_cart_contents[$rowid]['rowid'] = $rowid;		// 添加新項(xiàng)目		foreach ($items as $key => $val)		{			if ($key=='qty' && isset($_tmp_contents[$rowid][$key])) {				$this->_cart_contents[$rowid][$key] = $val+$_tmp_contents[$rowid][$key];			} else {				$this->_cart_contents[$rowid][$key] = $val;			}		}		return TRUE;	}	// --------------------------------	/**	 * 更新購(gòu)物車	 * 	 * @access	public	 * @param	array	 * @param	string	 * @return	bool	 */	function update($items = array())	{		// 驗(yàn)證		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		$save_cart = FALSE;		if (isset($items['rowid']) AND isset($items['qty']))		{			if ($this->_update($items) == TRUE)			{				$save_cart = TRUE;			}		}		else		{			foreach ($items as $val)			{				if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']))				{					if ($this->_update($val) == TRUE)					{						$save_cart = TRUE;					}				}			}		}		if ($save_cart == TRUE)		{			$this->_save_cart();			return TRUE;		}		return FALSE;	}	// --------------------------------	/**	 * 處理更新購(gòu)物車	 *	 * @access	private	 * @param	array	 * @return	bool	 */	function _update($items = array())	{		if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))		{			return FALSE;		}		// 檢測(cè)數(shù)量		$items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);		if ( ! is_numeric($items['qty']))		{			return FALSE;		}		if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'])		{			return FALSE;		}		if ($items['qty'] == 0)		{			unset($this->_cart_contents[$items['rowid']]);		}		else		{			$this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];		}		return TRUE;	}	// --------------------------------	/**	 * 保存購(gòu)物車到Session里	 *	 * @access	private	 * @return	bool	 */	function _save_cart()	{		unset($this->_cart_contents['total_items']);		unset($this->_cart_contents['cart_total']);		$total = 0;		$items = 0;		foreach ($this->_cart_contents as $key => $val)		{			if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))			{				continue;			}			$total += ($val['price'] * $val['qty']);			$items += $val['qty'];			$this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);		}		$this->_cart_contents['total_items'] = $items;		$this->_cart_contents['cart_total'] = $total;		if (count($this->_cart_contents) session('cart_contents', array());			return FALSE;		}		$this->session('cart_contents',$this->_cart_contents);		return TRUE;	}	// --------------------------------	/**	 * 購(gòu)物車中的總計(jì)金額	 *	 * @access	public	 * @return	integer	 */	function total()	{		return $this->_cart_contents['cart_total'];	}	// --------------------------------	/**	 * 購(gòu)物車中總共的項(xiàng)目數(shù)量	 *	 *	 * @access	public	 * @return	integer	 */	function total_items()	{		return $this->_cart_contents['total_items'];	}	// --------------------------------	/**	 * 購(gòu)物車中所有信息的數(shù)組	 *	 * 返回一個(gè)包含了購(gòu)物車中所有信息的數(shù)組	 *	 * @access	public	 * @return	array	 */	function contents()	{		$cart = $this->_cart_contents;		unset($cart['total_items']);		unset($cart['cart_total']);		return $cart;	}	// --------------------------------	/**	 * 購(gòu)物車中是否有特定的列包含選項(xiàng)信息	 *	 * 如果購(gòu)物車中特定的列包含選項(xiàng)信息,本函數(shù)會(huì)返回 TRUE(布爾值),本函數(shù)被設(shè)計(jì)為與 contents() 一起在循環(huán)中使用	 *	 * @access	public	 * @return	array	 */	function has_options($rowid = '')	{		if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0)		{			return FALSE;		}		return TRUE;	}	// --------------------------------	/**	 * 以數(shù)組的形式返回特定商品的選項(xiàng)信息	 *	 * 本函數(shù)被設(shè)計(jì)為與 contents() 一起在循環(huán)中使用	 *	 * @access	public	 * @return	array	 */	function product_options($rowid = '')	{		if ( ! isset($this->_cart_contents[$rowid]['options']))		{			return array();		}		return $this->_cart_contents[$rowid]['options'];	}	// --------------------------------	/**	 * 格式化數(shù)值	 *	 * 返回格式化后帶小數(shù)點(diǎn)的值(小數(shù)點(diǎn)后有2位),一般價(jià)格使用	 *	 * @access	public	 * @return	integer	 */	function format_number($n = '')	{		if ($n == '')		{			return '';		}		$n = trim(preg_replace('/([^0-9\.])/i', '', $n));		return number_format($n, 2, '.', ',');	}	// --------------------------------	/**	 * 銷毀購(gòu)物車	 *	 * 這個(gè)函數(shù)一般是在處理完用戶訂單后調(diào)用	 *	 * @access	public	 * @return	null	 */	function destroy()	{		unset($this->_cart_contents);		$this->_cart_contents['cart_total'] = 0;		$this->_cart_contents['total_items'] = 0;		$this->session('cart_contents', array());	}		// --------------------------------	/**	 * 保存Session	 *	 * 須有session_start();	 *	 * @access	private	 * @return	bool	 */	function session($name = 'cart_contents',$value = NULL) {		if ($name=='') $name = 'cart_contents';		if ($value == NULL) {			return @$_SESSION[$name];		} else {			if (!empty($value) && is_array($value)) {				$_SESSION[$name] = $value;				return TRUE;			} else {				return FALSE;			}		}	}}?>
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

C語(yǔ)言return的用法詳解 C語(yǔ)言return的用法詳解 Oct 07, 2023 am 10:58 AM

C語(yǔ)言return的用法有:1、對(duì)於傳回值類型為void的函數(shù),可以使用return語(yǔ)句來(lái)提前結(jié)束函數(shù)的執(zhí)行;2、對(duì)於傳回值型別不為void的函數(shù),return語(yǔ)句的作用是將函數(shù)的執(zhí)行結(jié)果傳回給呼叫者;3、提前結(jié)束函數(shù)的執(zhí)行,在函數(shù)內(nèi)部,我們可以使用return語(yǔ)句來(lái)提前結(jié)束函數(shù)的執(zhí)行,即使函數(shù)並沒(méi)有回傳值。

Java中return和finally語(yǔ)句的執(zhí)行順序是怎樣的? Java中return和finally語(yǔ)句的執(zhí)行順序是怎樣的? Apr 25, 2023 pm 07:55 PM

原始碼:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}#輸出上述程式碼的輸出可以簡(jiǎn)單地得出結(jié)論:return在finally之前執(zhí)行,我們來(lái)看下字節(jié)碼層面上發(fā)生了什麼事情。下面截取case1方法的部分字節(jié)碼,並且對(duì)照源碼,將每個(gè)指令的含義註釋在

聊聊Vue2為什麼能透過(guò)this存取各種選項(xiàng)中屬性 聊聊Vue2為什麼能透過(guò)this存取各種選項(xiàng)中屬性 Dec 08, 2022 pm 08:22 PM

這篇文章帶大家解讀vue原始碼,來(lái)介紹一下Vue2中為什麼可以使用 this 存取各種選項(xiàng)中的屬性,希望對(duì)大家有幫助!

使用JavaScript中return關(guān)鍵字 使用JavaScript中return關(guān)鍵字 Feb 18, 2024 pm 12:45 PM

JavaScript中return的用法,需要具體程式碼範(fàn)例在JavaScript中,return語(yǔ)句用來(lái)指定從函數(shù)傳回的值。它不僅可以用於結(jié)束函數(shù)的執(zhí)行,還可以將一個(gè)值傳回給呼叫函數(shù)的地方。 return語(yǔ)句有以下幾個(gè)常見的用法:傳回一個(gè)值return語(yǔ)句可以用來(lái)傳回一個(gè)值給呼叫函數(shù)的地方。下面是一個(gè)簡(jiǎn)單的範(fàn)例:functionadd(a,b){

Vue3怎麼使用setup語(yǔ)法糖拒絕寫return Vue3怎麼使用setup語(yǔ)法糖拒絕寫return May 12, 2023 pm 06:34 PM

Vue3.2setup語(yǔ)法糖是在單文件組件(SFC)中使用組合式API的編譯時(shí)語(yǔ)法糖解決Vue3.0中setup需要繁瑣將聲明的變量、函數(shù)以及import引入的內(nèi)容通過(guò)return向外暴露,才能在使用的問(wèn)題1.在使用中無(wú)需return宣告的變數(shù)、函數(shù)以及import引入的內(nèi)容,即可在使用語(yǔ)法糖//import引入的內(nèi)容import{getToday}from'./utils'//變數(shù)constmsg='Hello !'//函數(shù)func

詳解JavaScript函數(shù)傳回值和return語(yǔ)句 詳解JavaScript函數(shù)傳回值和return語(yǔ)句 Aug 04, 2022 am 09:46 AM

JavaScript 函數(shù)提供兩個(gè)介面實(shí)現(xiàn)與外界的交互,其中參數(shù)作為入口,接收外界資訊;返回值作為出口,並將運(yùn)算結(jié)果回饋給外界。以下這篇文章帶大家了解JavaScript函數(shù)回傳值,淺析下return語(yǔ)句的用法,希望對(duì)大家有幫助!

一篇搞懂this指向,追趕70%的前端人 一篇搞懂this指向,追趕70%的前端人 Sep 06, 2022 pm 05:03 PM

同事因?yàn)閠his指向的問(wèn)題卡住的bug,vue2的this指向問(wèn)題,使用了箭頭函數(shù),導(dǎo)致拿不到對(duì)應(yīng)的props。當(dāng)我跟他介紹的時(shí)候他竟然不知道,隨後也刻意的看了一下前端交流群,至今最起碼還有70%以上的前端程式設(shè)計(jì)師搞不明白,今天給大家分享一下this指向,如果啥都沒(méi)學(xué)會(huì),請(qǐng)給我一個(gè)大嘴巴子。

Python回傳值return怎麼用 Python回傳值return怎麼用 Oct 07, 2023 am 11:10 AM

Python回傳值return用法是當(dāng)函數(shù)執(zhí)行到return語(yǔ)句時(shí),就會(huì)立即停止執(zhí)行,並將指定的值傳回給呼叫函數(shù)的地方。詳細(xì)用法:1、傳回單一值;2、傳回多個(gè)值;3、傳回空值;4、提前結(jié)束函數(shù)的執(zhí)行。

See all articles