⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cart.php

📁 完善的PHP/MySQL电子商务方案
💻 PHP
字号:
<?/* cart.php (c) 2000 Ying Zhang (ying@zippydesign.com) * * TERMS OF USAGE: * This file was written and developed by Ying Zhang (ying@zippydesign.com) * for educational and demonstration purposes only.  You are hereby granted the * rights to use, modify, and redistribute this file as you like.  The only * requirement is that you must retain this notice, without modifications, at * the top of your source code.  No warranties or guarantees are expressed or * implied. DO NOT use this code in a production environment without * understanding the limitations and weaknesses pretaining to or caused by the * use of these scripts, directly or indirectly. USE AT YOUR OWN RISK! */class Cart {	var $items;		/* array of items */	var $total;		/* total of the cart */	function Cart() {	/* object constructor */		$this->init();	}	function init() {	/* this function is called to initialize (and reset) a shopping cart */		$this->items = array();		$this->total = 0;	}	function add(&$productid, $qty) {	/* add an item to the shopping cart and update the total price */		if (isset($productid)) {			setdefault($this->items[$productid], 0);			$this->items[$productid] += $qty;		}	}	function set(&$productid, $qty) {	/* set the quantity of a product in the cart to a specified value */		if (isset($productid)) {			$this->items[$productid] = (int) $qty;		}	}	function remove(&$productid) {	/* this function will remove a given product from the cart */		if (isset($productid)) {			unset($this->items[$productid]);		}	}	function cleanup() {	/* this function will clean up the cart, removing items with invalid product	 * id's (non-numeric ones) and products with quantities less than 1 */		foreach ($this->items as $productid => $qty) {			if ($qty < 1) {				unset($this->items[$productid]);			}		}	}	function itemcount() {	/* returns the number of individual items in the shopping cart (note, this	 * takes into account the quantities of the items as well) */		$count = 0;		foreach ($this->items as $productid => $qty) {			$count += $qty;		}		return $count;	}	function get_productid_list() {	/* return a comma delimited list of all the products in the cart, this will	 * be used for queries, eg. SELECT id, price FROM products WHERE id IN ....     */		$productid_list = "";		foreach ($this->items as $productid => $qty) {			$productid_list .= ",'" . $productid . "'";		}		/* need to strip off the leading comma */		return substr($productid_list, 1);	}	function recalc_total() {	/* recalculate the total for the shopping cart, we will also do some cleanup	 * and remove invalid items from the cart.  we have to query the database to	 * get the prices, so instead of making one query for each product in the	 * basket, we will gather up all the ID's we are interested in and run one	 * query to get all the products we care about (using $in_clause) */		$this->total = 0;		$in_clause = $this->get_productid_list();		if (empty($in_clause)) {			return;		}		$qid = db_query("SELECT id, price FROM products WHERE id IN ($in_clause)");		while ($product = db_fetch_object($qid)) {			$this->total += $this->items[$product->id] * $product->price;		}	}}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -