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

📄 products.php

📁 完善的PHP/MySQL电子商务方案
💻 PHP
字号:
<?/* products.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! *//****************************************************************************** * MAIN *****************************************************************************/include("../application.php");require_login();require_priv("admin");$DOC_TITLE = "MyMarket Product Management";include("templates/header.php");switch (nvl($mode)) {	case "add" :		print_add_product_form(nvl($category_id, 0));		break;	case "edit" :		print_edit_product_form($id);		break;	case "del" :		delete_product($id);		print_product_list();		break;	case "insert" :		insert_product($id, $HTTP_POST_VARS);		print_product_list();		break;	case "update" :		update_product($id, $HTTP_POST_VARS);		print_product_list();		break;	default :		print_product_list();		break;}include("templates/footer.php");/****************************************************************************** * FUNCTIONS *****************************************************************************/function print_add_product_form($category_id = 0) {/* print a blank product form so we can add a new product */	global $CFG, $ME;	/* set default values for the reset of the fields */	$frm["categories"] = array($category_id);	$frm["newmode"] = "insert";	$frm["name"] = "";	$frm["description"] = "";	$frm["price"] = "";	$frm["on_special"] = "";	$frm["submit_caption"] = "Add Product";	/* build the categories listbox options, preselect the top item */	build_category_tree($category_options, $frm["categories"]);	include("templates/product_form.php");}function print_edit_product_form($id) {/* print a product form so we can edit the selected product */	global $CFG, $ME;	/* load up the information for the product */	$qid = db_query("	SELECT name, description, price, on_special	FROM products	WHERE id = $id	");	$frm = db_fetch_array($qid);	/* load up the categories for the product */	$qid = db_query("	SELECT category_id	FROM products_categories	WHERE product_id = $id	");	$frm["categories"] = array();	while ($cat = db_fetch_object($qid)) {		$frm["categories"][] = $cat->category_id;	}	/* set values for the form */	$frm["newmode"] = "update";	$frm["submit_caption"] = "Save Changes";	/* build the categories listbox options, preselect the selected item */	build_category_tree($category_options, $frm["categories"]);	include("templates/product_form.php");}function delete_product($id) {/* delete the product specified by $id, we have to delete the product and then * the appropriate entries from the products_categories table.  this should be * wrapped inside a transaction, unfortunately MySQL currently does not support * transactions. */	global $CFG, $ME;	/* load up the information for the product */	$qid = db_query("	SELECT name	FROM products	WHERE id = $id	");	$prod = db_fetch_object($qid);	/* delete this product */	$qid = db_query("	DELETE FROM products	WHERE id = $id	");	/* delete this product from the products_categories table */	$qid = db_query("	DELETE FROM products_categories	WHERE product_id = $id	");	include("templates/product_deleted.php");}function insert_product($id, $frm) {/* add a new subproduct under the parent $id.  all the fields that we want are * going to in the variable $frm */	global $CFG, $ME;	$on_special = checked($frm["on_special"]);	/* add the product into the products table */	$qid = db_query("	INSERT INTO products (name, description, price, on_special)	VALUES ('$frm[name]', '$frm[description]', '$frm[price]', '$on_special')	");	/* get the product id that was just created */	$product_id = db_insert_id();	/* add this product under the specified categories */	for ($i = 0; $i < count($frm["categories"]); $i++) {		$qid = db_query("		INSERT INTO products_categories (category_id, product_id)		VALUES ('{$frm["categories"][$i]}', '$product_id')		");	}}function update_product($id, $frm) {/* update the product $id with new values.  all the fields that we want are * going to in the variable $frm */	global $CFG, $ME;	checked($frm["on_special"]);	/* update the products table with the new information */	$qid = db_query("	UPDATE products SET		 name = '$frm[name]'		,description = '$frm[description]'		,price = '$frm[price]'		,on_special = '$frm[on_special]'	WHERE id = $id	");	/* delete all the categories this product was associated with */	$qid = db_query("	DELETE FROM products_categories	WHERE product_id = $id	");	/* add associations for all the categories this product belongs to, if	 * no categories were selected, we will make it belong to the top	 * category */	if (count($frm["categories"]) == 0) {		$frm["categories"][] = 0;	}	for ($i = 0; $i < count($frm["categories"]); $i++) {		$qid = db_query("		INSERT INTO products_categories (category_id, product_id)		VALUES ('{$frm["categories"][$i]}', '$id')		");	}}function print_product_list() {/* read all the categories from the database and print them into a table.  we * will use a template to display the listings to keep this main script clean */	global $CFG, $ME;	$qid = db_query("	SELECT p.id, p.name, p.description, p.price, c.name AS category	FROM products p, products_categories pc, categories c	WHERE p.id = pc.product_id		  AND c.id = pc.category_id  	");	include("templates/product_list.php");}?>

⌨️ 快捷键说明

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