Beginning PHP and MySQL E-Commerce From Novice to Professional phần 6 doc

74 351 0
Beginning PHP and MySQL E-Commerce From Novice to Professional phần 6 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

■Note A complete description of the $_FILES superglobal is available at http://www.php.net/manual/ en/features.file-upload.php. The move_uploaded_file() PHP function is used to move the file from the temporary location to the product_images folder: /* Use the move_uploaded_file PHP function to move the file from its temporary location to the product_images folder */ move_uploaded_file($_FILES['ImageUpload']['tmp_name'], SITE_ROOT . '/product_images/' . $_FILES['ImageUpload']['name']); After uploading a product picture, the file name must be stored in the database (otherwise, the file upload has no effect): // Update the product's information in the database Catalog::SetImage($this->_mProductId, $_FILES['ImageUpload']['name']); As you can see, it’s pretty simple to handle file uploads with PHP. Product Details: Implementing the Business Tier To implement the business tier, you’ll need to add the following methods to the Catalog class: • UpdateProduct updates a product’s details: name, description, price, and discounted price. • DeleteProduct completely removes a product from the catalog. • RemoveProductFromCategory is called when the “Remove from category” button is clicked to unassign the product from a category. • GetCategories returns all the categories from our catalog. • GetProductInfo returns the product details. • GetCategoriesForProduct is used to get the list of categories that are related to the spec- ified product. • SetProductDisplayOption sets the product’s display setting. • AssignProductToCategory assigns a product to a category. • MoveProductToCategory moves a product from one category to another. • GetAttributesNotAssignedToProduct returns all the attribute values from the table of attribute_values that have not been assigned to a product. CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES 341 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 341 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com • AssignAttributeValueToProduct assigns an attribute value to a product. • RemoveProductAttributeValue removes the association between a product and an attribute value from the product_attribute table. • SetImage1 changes the image file name in the database for a certain product. • SetImage2 changes the second image file name in the database for a certain product. • SetThumbnail changes the thumbnail image file name for a certain product. Exercise: Implementing the Business Tier Methods Because the functionality is better expressed by the data tier functions that the methods call, we’ll discuss them in more detail when we implement the data tier. For now, simply add the following code to the Catalog class, in business/catalog.php: // Updates a product public static function UpdateProduct($productId, $productName, $productDescription, $productPrice, $productDiscountedPrice) { // Build the SQL query $sql = 'CALL catalog_update_product(:product_id, :product_name, :product_description, :product_price, :product_discounted_price)'; // Build the parameters array $params = array (':product_id' => $productId, ':product_name' => $productName, ':product_description' => $productDescription, ':product_price' => $productPrice, ':product_discounted_price' => $productDiscountedPrice); // Execute the query DatabaseHandler::Execute($sql, $params); } // Removes a product from the product catalog public static function DeleteProduct($productId) { // Build SQL query $sql = 'CALL catalog_delete_product(:product_id)'; // Build the parameters array $params = array (':product_id' => $productId); CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES342 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 342 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com // Execute the query DatabaseHandler::Execute($sql, $params); } // Unassigns a product from a category public static function RemoveProductFromCategory($productId, $categoryId) { // Build SQL query $sql = 'CALL catalog_remove_product_from_category( :product_id, :category_id)'; // Build the parameters array $params = array (':product_id' => $productId, ':category_id' => $categoryId); // Execute the query and return the results return DatabaseHandler::GetOne($sql, $params); } // Retrieves the list of categories a product belongs to public static function GetCategories() { // Build SQL query $sql = 'CALL catalog_get_categories()'; // Execute the query and return the results return DatabaseHandler::GetAll($sql); } // Retrieves product info public static function GetProductInfo($productId) { // Build SQL query $sql = 'CALL catalog_get_product_info(:product_id)'; // Build the parameters array $params = array (':product_id' => $productId); // Execute the query and return the results return DatabaseHandler::GetRow($sql, $params); } // Retrieves the list of categories a product belongs to public static function GetCategoriesForProduct($productId) { // Build SQL query $sql = 'CALL catalog_get_categories_for_product(:product_id)'; CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES 343 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 343 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com // Build the parameters array $params = array (':product_id' => $productId); // Execute the query and return the results return DatabaseHandler::GetAll($sql, $params); } // Assigns a product to a category public static function SetProductDisplayOption($productId, $display) { // Build SQL query $sql = 'CALL catalog_set_product_display_option( :product_id, :display)'; // Build the parameters array $params = array (':product_id' => $productId, ':display' => $display); // Execute the query DatabaseHandler::Execute($sql, $params); } // Assigns a product to a category public static function AssignProductToCategory($productId, $categoryId) { // Build SQL query $sql = 'CALL catalog_assign_product_to_category( :product_id, :category_id)'; // Build the parameters array $params = array (':product_id' => $productId, ':category_id' => $categoryId); // Execute the query DatabaseHandler::Execute($sql, $params); } // Moves a product from one category to another public static function MoveProductToCategory($productId, $sourceCategoryId, $targetCategoryId) { // Build SQL query $sql = 'CALL catalog_move_product_to_category(:product_id, :source_category_id, :target_category_id)'; // Build the parameters array $params = array (':product_id' => $productId, CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES344 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 344 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ':source_category_id' => $sourceCategoryId, ':target_category_id' => $targetCategoryId); // Execute the query DatabaseHandler::Execute($sql, $params); } // Gets the catalog attributes that are not assigned to the specified product public static function GetAttributesNotAssignedToProduct($productId) { // Build the SQL query $sql = 'CALL catalog_get_attributes_not_assigned_to_product(:product_id)'; // Build the parameters array $params = array (':product_id' => $productId); // Execute the query and return the results return DatabaseHandler::GetAll($sql, $params); } // Assign an attribute value to the specified product public static function AssignAttributeValueToProduct($productId, $attributeValueId) { // Build SQL query $sql = 'CALL catalog_assign_attribute_value_to_product( :product_id, :attribute_value_id)'; // Build the parameters array $params = array (':product_id' => $productId, ':attribute_value_id' => $attributeValueId); // Execute the query DatabaseHandler::Execute($sql, $params); } // Removes a product attribute value public static function RemoveProductAttributeValue($productId, $attributeValueId) { // Build SQL query $sql = 'CALL catalog_remove_product_attribute_value( :product_id, :attribute_value_id)'; // Build the parameters array $params = array (':product_id' => $productId, ':attribute_value_id' => $attributeValueId); CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES 345 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 345 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com // Execute the query DatabaseHandler::Execute($sql, $params); } // Changes the name of the product image file in the database public static function SetImage($productId, $imageName) { // Build SQL query $sql = 'CALL catalog_set_image(:product_id, :image_name)'; // Build the parameters array $params = array (':product_id' => $productId, ':image_name' => $imageName); // Execute the query DatabaseHandler::Execute($sql, $params); } // Changes the name of the second product image file in the database public static function SetImage2($productId, $imageName) { // Build SQL query $sql = 'CALL catalog_set_image_2(:product_id, :image_name)'; // Build the parameters array $params = array (':product_id' => $productId, ':image_name' => $imageName); // Execute the query DatabaseHandler::Execute($sql, $params); } // Changes the name of the product thumbnail file in the database public static function SetThumbnail($productId, $thumbnailName) { // Build SQL query $sql = 'CALL catalog_set_thumbnail(:product_id, :thumbnail_name)'; // Build the parameters array $params = array (':product_id' => $productId, ':thumbnail_name' => $thumbnailName); // Execute the query DatabaseHandler::Execute($sql, $params); } CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES346 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 346 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Product Details: Implementing the Data Tier In the data tier, you add the stored procedures that correspond to the business tier methods in the Catalog class you have just seen. Exercise: Adding the Stored Procedures 1. Use phpMyAdmin to execute and create the stored procedures described in the following steps, and don’t forget to set $$ as the delimiter before executing the code. 2. Execute the following code, which creates the catalog_update_product stored procedure to your tshirtshop database. The catalog_update_product stored procedure updates the details of a product using the data received through the inProductId, inName, inDescription, inPrice, and inDiscountedPrice input parameters. Create catalog_update_product stored procedure CREATE PROCEDURE catalog_update_product(IN inProductId INT, IN inName VARCHAR(100), IN inDescription VARCHAR(1000), IN inPrice DECIMAL(10, 2), IN inDiscountedPrice DECIMAL(10, 2)) BEGIN UPDATE product SET name = inName, description = inDescription, price = inPrice, discounted_price = inDiscountedPrice WHERE product_id = inProductId; END$$ 3. Execute the following code, which creates the catalog_delete_product stored procedure to your tshirtshop database. The catalog_delete_product stored procedure completely removes a product from the catalog by deleting its entries in the product_attribute, product_category, and product tables. Create catalog_delete_product stored procedure CREATE PROCEDURE catalog_delete_product(IN inProductId INT) BEGIN DELETE FROM product_attribute WHERE product_id = inProductId; DELETE FROM product_category WHERE product_id = inProductId; DELETE FROM product WHERE product_id = inProductId; END$$ 4. Execute the following code, which creates the catalog_remove_product_from_category stored procedure in your tshirtshop database. The catalog_remove_product_from_category stored pro- cedure verifies how many categories the product exists in. If the product exists in more than one category, it just removes the product from the specified category (ID received as a parameter). If the product is associ- ated with a single category, it is removed completely from the database. Create catalog_remove_product_from_category stored procedure CREATE PROCEDURE catalog_remove_product_from_category( IN inProductId INT, IN inCategoryId INT) CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES 347 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 347 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com BEGIN DECLARE productCategoryRowsCount INT; SELECT count(*) FROM product_category WHERE product_id = inProductId INTO productCategoryRowsCount; IF productCategoryRowsCount = 1 THEN CALL catalog_delete_product(inProductId); SELECT 0; ELSE DELETE FROM product_category WHERE category_id = inCategoryId AND product_id = inProductId; SELECT 1; END IF; END$$ 5. Execute the following code, which creates the catalog_get_categories stored procedure in your tshirtshop database; catalog_get_categories simply returns all the categories from your catalog. Create catalog_get_categories stored procedure CREATE PROCEDURE catalog_get_categories() BEGIN SELECT category_id, name, description FROM category ORDER BY category_id; END$$ 6. Execute the following code, which creates the catalog_get_product_info stored procedure in your tshirtshop database. The catalog_get_product_info stored procedure retrieves the product name, description, price, discounted price, image, the second image, thumbnail, and display option for the product identified by the product ID (inProductId). Create catalog_get_product_info stored procedure CREATE PROCEDURE catalog_get_product_info(IN inProductId INT) BEGIN SELECT product_id, name, description, price, discounted_price, image, image_2, thumbnail, display FROM product WHERE product_id = inProductId; END$$ 7. Execute this code, which creates the catalog_get_categories_for_product stored procedure in your tshirtshop database. The catalog_get_categories_for_product stored procedure returns a list of the categories that belong to the specified product. Only their IDs and names are returned, because this is the only information we’re interested in. CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES348 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 348 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Create catalog_get_categories_for_product stored procedure CREATE PROCEDURE catalog_get_categories_for_product(IN inProductId INT) BEGIN SELECT c.category_id, c.department_id, c.name FROM category c JOIN product_category pc ON c.category_id = pc.category_id WHERE pc.product_id = inProductId ORDER BY category_id; END$$ 8. Execute this code, which creates the catalog_set_product_display_option stored procedure in your tshirtshop database: Create catalog_set_product_display_option stored procedure CREATE PROCEDURE catalog_set_product_display_option( IN inProductId INT, IN inDisplay SMALLINT) BEGIN UPDATE product SET display = inDisplay WHERE product_id = inProductId; END$$ 9. Execute the following code, which creates the catalog_assign_product_to_category stored proce- dure in your tshirtshop database. The catalog_assign_product_to_category stored procedure associates a product with a category by adding a (product_id, category_id) value pair into the product_ category table. Create catalog_assign_product_to_category stored procedure CREATE PROCEDURE catalog_assign_product_to_category( IN inProductId INT, IN inCategoryId INT) BEGIN INSERT INTO product_category (product_id, category_id) VALUES (inProductId, inCategoryId); END$$ 10. Execute the following code, which creates the catalog_assign_product_to_category stored proce- dure in your tshirtshop database. The catalog_move_product_to_category stored procedure removes a product from a category and places it in another one. Create catalog_move_product_to_category stored procedure CREATE PROCEDURE catalog_move_product_to_category(IN inProductId INT, IN inSourceCategoryId INT, IN inTargetCategoryId INT) BEGIN UPDATE product_category SET category_id = inTargetCategoryId WHERE product_id = inProductId AND category_id = inSourceCategoryId; END$$ CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES 349 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 349 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 11. Execute the following code, which creates the catalog_get_attributes_not_assigned_to_product stored procedure in your tshirtshop database. This procedure returns all attribute values that weren’t already associated with the product in the product_attribute table. Create catalog_get_attributes_not_assigned_to_product stored procedure CREATE PROCEDURE catalog_get_attributes_not_assigned_to_product( IN inProductId INT) BEGIN SELECT a.name AS attribute_name, av.attribute_value_id, av.value AS attribute_value FROM attribute_value av INNER JOIN attribute a ON av.attribute_id = a.attribute_id WHERE av.attribute_value_id NOT IN (SELECT attribute_value_id FROM product_attribute WHERE product_id = inProductId) ORDER BY attribute_name, av.attribute_value_id; END$$ 12. Execute the following code, which creates the catalog_assign_attribute_value_to_product stored procedure in your tshirtshop database. This procedure assigns an attribute value to a product by adding a new record to the product_attribute table. Create catalog_assign_attribute_value_to_product stored procedure CREATE PROCEDURE catalog_assign_attribute_value_to_product( IN inProductId INT, IN inAttributeValueId INT) BEGIN INSERT INTO product_attribute (product_id, attribute_value_id) VALUES (inProductId, inAttributeValueId); END$$ 13. Execute the following code, which creates the catalog_remove_product_attribute_value stored procedure in your tshirtshop database. This procedure unassigns an attribute value from a product by deleting the necessary record from the product_attribute table. Create catalog_remove_product_attribute_value stored procedure CREATE PROCEDURE catalog_remove_product_attribute_value( IN inProductId INT, IN inAttributeValueId INT) BEGIN DELETE FROM product_attribute WHERE product_id = inProductId AND attribute_value_id = inAttributeValueId; END$$ CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES350 8644ch11FINAL.qxd 1/30/08 12:37 PM Page 350 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... presentation/admin_menu .php, and modify the constructor of the AdminMenu class like this: public function construct() { $this->mLinkToStoreAdmin = Link::ToAdmin(); $this->mLinkToAttributesAdmin = Link::ToAttributesAdmin(); if (isset ($_SESSION['link _to_ store_front'])) $this->mLinkToStoreFront = $_SESSION['link _to_ store_front']; else $this->mLinkToStoreFront = Link::ToIndex(); $this->mLinkToLogout = Link::ToLogout(); } 16. .. control and which cannot be easily saved into your database for further processing and analysis With the custom shopping cart, when the visitor clicks the Add to Cart button for a product, the product is still added to the visitor’s shopping cart, but this cart and product information will be stored directly in the tshirtshop database rather than the inaccessible PayPal database When the visitor clicks... little bit easier This feature consists of Edit buttons, such as the Edit Department Details and Edit Product Details buttons you can see in Figure 11 -6 These buttons show up only if the visitor is authenticated as an administrator and take him or her directly to the item administrative page 351 864 4ch11FINAL.qxd 1/30/08 12:37 PM Page 352 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com... administrator, and notice the Edit buttons that show up, as shown in Figures 11 -6 and 11-7 864 4ch11FINAL.qxd 1/30/08 12:37 PM Page 359 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES Figure 11-7 Administering product details How It Works: In-Store Administration Links In this exercise, we created Edit buttons throughout... cookies to keep track of shopping carts When the visitor clicks the Add to Cart button, the server first verifies whether a shopping cart cookie already exists on the visitor’s computer If it does, the specified product is added to the existing cart Otherwise, the server generates a unique cart ID, saves it to the client’s cookie, and then adds the product to the newly generated shopping cart Storing... $this->mEditActionTarget = Link::ToDepartmentsAdmin(); $this->mEditAction = 'edit_dept_' $this->_mDepartmentId; $this->mEditButtonCaption = 'Edit Department Details'; } } } ?> 6 Add the following piece of code to presentation/templates/product.tpl: {* Add the submit button and close the form *} {* Show edit button for administrators *} {if $obj->mShowEditButton}... shopping cart you’ve been using so far 864 4ch12FINAL.qxd 1/30/08 12:38 PM Page 361 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com PART 2 ■■■ Phase II of Development 864 4ch12FINAL.qxd 1/30/08 12:38 PM Page 362 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 864 4ch12FINAL.qxd 1/30/08 12:38 PM Page 363 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com... page that allows site administrators to delete shopping carts that weren’t updated in a specified number of days 363 864 4ch12FINAL.qxd 1/30/08 12:38 PM Page 364 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 364 CHAPTER 12 ■ CREATING YOUR OWN SHOPPING CART Designing the Shopping Cart In this chapter we will implement a custom shopping cart, which stores data in the local tshirtshop... htmlspecialchars_decode( Link::ToProductAdmin($department_id, $category_id, $this->_mProductId))); 355 864 4ch11FINAL.qxd 1/30/08 12:37 PM Page 3 56 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 3 56 CHAPTER 11 ■ CATALOG ADMINISTRATION: PRODUCTS AND ATTRIBUTES } } } } ?> 10 Add the following piece of code to presentation/templates/products_list.tpl This adds the Edit buttons to the product... later” feature Figure 12-3 The TShirtShop shopping cart summary 365 864 4ch12FINAL.qxd 1/30/08 12:38 PM Page 366 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 366 CHAPTER 12 ■ CREATING YOUR OWN SHOPPING CART Before starting to write the code for the shopping cart, let’s take a closer look at what we’re going to do First, note that you won’t have any user personalization features . ($_SESSION['link _to_ store_front'])) $this->mLinkToStoreFront = $_SESSION['link _to_ store_front']; else $this->mLinkToStoreFront = Link::ToIndex(); $this->mLinkToLogout = Link::ToLogout(); } 16. . presentation/admin_menu .php, and modify the constructor of the AdminMenu class like this: public function __construct() { $this->mLinkToStoreAdmin = Link::ToAdmin(); $this->mLinkToAttributesAdmin = Link::ToAttributesAdmin(); if. Department Details and Edit Product Details buttons you can see in Figure 11 -6. These buttons show up only if the visitor is authenticated as an administrator and take him or her directly to the item

Ngày đăng: 12/08/2014, 10:21

Mục lục

  • Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition

  • Contents at a Glance

  • About the Technical Reviewers

  • Introduction

    • Who This Book Is For

    • How This Book Is Structured

      • Phase I of Development

        • Chapter 1: Starting an E-Commerce Site

        • Chapter 2: Laying Out the Foundations

        • Chapter 3: Starting the TShirtShop Project

        • Chapter 4: Creating the Product Catalog: Part 1

        • Chapter 5: Creating the Product Catalog: Part 2

        • Chapter 7: Search Engine Optimization

        • Chapter 8: Searching the Catalog

        • Chapter 9: Receiving Payments Using PayPal

        • Chapter 10: Catalog Administration: Departments and Categories

        • Chapter 11: Catalog Administration: Products and Attributes

        • Phase II of Development

          • Chapter 12: Creating Your Own Shopping Cart

          • Chapter 13: Implementing AJAX Features

          • Chapter 14: Accepting Customer Orders

          • Phase III of Development

            • Chapter 16: Managing Customer Details

            • Chapter 17: Storing Customer Orders

            • Chapter 18: Implementing the Order Pipeline: Part 1

Tài liệu cùng người dùng

Tài liệu liên quan