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

Home Backend Development PHP Tutorial PHP購物車種,移植于CodeIgniter

PHP購物車種,移植于CodeIgniter

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

PHP購物車類,移植于CodeIgniter

<?php /** * 購物車程序 Modified by CodeIgniter * */class cart {	// 對產(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;		}	}	// --------------------------------	/**	 * 添加到購物車	 *	 * @access	public	 * @param	array	 * @return	bool	 */	function insert($items = array())	{		// 檢測數(shù)據(jù)是否正確		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		// 可以添加一個商品(一維數(shù)組),也可以添加多個商品(二維數(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;	}	// --------------------------------	/**	 * 處理插入購物車數(shù)據(jù)	 *	 * @access	private	 * @param	array	 * @return	bool	 */	function _insert($items = array())	{		// 檢查購物車		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		// --------------------------------		/* 前四個數(shù)組索引 (id, qty, price 和name) 是 必需的。		   如果缺少其中的任何一個,數(shù)據(jù)將不會被保存到購物車中。		   第5個索引 (options) 是可選的。當(dāng)你的商品包含一些相關(guān)的選項(xiàng)信息時,你就可以使用它。		   請使用一個數(shù)組來保存選項(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;		}		*/		// --------------------------------		// 價格驗(yàn)證		$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));		$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));		// 驗(yàn)證價格是否是數(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		{			// 沒有屬性時直接對產(chǎn)品ID加密			$rowid = md5($items['id']);		}				// 檢測購物車中是否有該產(chǎn)品,如果有,在原來的基礎(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;			}		}		// --------------------------------		// 清除原來的數(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;	}	// --------------------------------	/**	 * 更新購物車	 * 	 * @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;	}	// --------------------------------	/**	 * 處理更新購物車	 *	 * @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;		}		// 檢測數(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;	}	// --------------------------------	/**	 * 保存購物車到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;	}	// --------------------------------	/**	 * 購物車中的總計(jì)金額	 *	 * @access	public	 * @return	integer	 */	function total()	{		return $this->_cart_contents['cart_total'];	}	// --------------------------------	/**	 * 購物車中總共的項(xiàng)目數(shù)量	 *	 *	 * @access	public	 * @return	integer	 */	function total_items()	{		return $this->_cart_contents['total_items'];	}	// --------------------------------	/**	 * 購物車中所有信息的數(shù)組	 *	 * 返回一個包含了購物車中所有信息的數(shù)組	 *	 * @access	public	 * @return	array	 */	function contents()	{		$cart = $this->_cart_contents;		unset($cart['total_items']);		unset($cart['cart_total']);		return $cart;	}	// --------------------------------	/**	 * 購物車中是否有特定的列包含選項(xiàng)信息	 *	 * 如果購物車中特定的列包含選項(xiàng)信息,本函數(shù)會返回 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位),一般價格使用	 *	 * @access	public	 * @return	integer	 */	function format_number($n = '')	{		if ($n == '')		{			return '';		}		$n = trim(preg_replace('/([^0-9\.])/i', '', $n));		return number_format($n, 2, '.', ',');	}	// --------------------------------	/**	 * 銷毀購物車	 *	 * 這個函數(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;			}		}	}}?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Use the return keyword in JavaScript Use the return keyword in JavaScript Feb 18, 2024 pm 12:45 PM

Usage of return in JavaScript requires specific code examples In JavaScript, the return statement is used to specify the value returned from a function. Not only can it be used to end the execution of a function, it can also return a value to the place where the function was called. The return statement has the following common uses: Return a value The return statement can be used to return a value to the place where the function is called. Here is a simple example: functionadd(a,b){

How does Vue3 use setup syntax sugar to refuse to write return How does Vue3 use setup syntax sugar to refuse to write return May 12, 2023 pm 06:34 PM

Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func

Detailed explanation of JavaScript function return values ??and return statements Detailed explanation of JavaScript function return values ??and return statements Aug 04, 2022 am 09:46 AM

JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

How to use return value in Python How to use return value in Python Oct 07, 2023 am 11:10 AM

Python return value return usage is that when the function executes the return statement, execution will stop immediately and the specified value will be returned to the place where the function was called. Detailed usage: 1. Return a single value; 2. Return multiple values; 3. Return a null value; 4. End the execution of the function early.

See all articles