PHP and MySQL Web Development - P112 pptx

5 194 0
PHP and MySQL Web Development - P112 pptx

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

Thông tin tài liệu

527 Implementing the Shopping Cart to get the book information out of the database, and display_book_details($book); to output the data in HTML. One thing to note here is that display_book_details() looks for an image file for the book as images/$isbn.jpg. If this file does not exist, no image will be displayed. The remainder of the script sets up navigation.A normal user will have the choices Continue Shopping, which will take her back to the category page, and Add to Cart, which will add the book to her shopping cart. If a user is logged in as an administrator, she will get some different options, which we’ll look at in the section on administration. That completes the basics of the catalog system. Let’s go ahead and look at the code for the shopping cart functionality. Implementing the Shopping Cart The shopping cart functionality all revolves around a session variable called cart.This is an associative array that has ISBNs as keys and quantities as values. For example, if I add a single copy of this book to my shopping cart, the array would contain 0672317842 => 1 That is, one copy of the book with the ISBN 0672317842.When we add items to the cart, they will be added to the array.When we view the cart, we will use the cart array to look up the full details of the items in the database. We also use two other session variables to control the display in the header that shows Total Items and Total Price.These variables are called items and total_price,respec- tively. Using the show_cart.php Script Let’s begin looking at how the shopping cart code is implemented by looking at the show_cart.php script.This is the script that displays the page we will visit if we click on any View Cart or Add to Cart links. If we call show_cart.php without any parameters, we will get to see the contents of it. If we call it with an ISBN as parameter, the item with that ISBN will be added to the cart. To understand this fully, look first at Figure 25.6. In this case, we have clicked the View Cart link when our cart is empty; that is, we have not yet selected any items to purchase. Figure 25.7 shows our cart a bit further down the track when we have selected two books to buy. In this case, we have gotten to this page by clicking the Add to Cart link on the show_book.php page for this book, PHP and MySQL Web Development. If you look closely at the URL bar, you will see that we have called the script with a parameter this time.The parameter is called new and has the value 0672317842—that is, the ISBN for the book we have just added to the cart. 31 525x ch25 1/24/03 3:39 PM Page 527 528 Chapter 25 Building a Shopping Cart Figure 25.7 The show_cart.php script with the new parameter adds a new item to the cart. Figure 25.6 The show_cart.php script with no parameters just shows us the contents of our cart. 31 525x ch25 1/24/03 3:39 PM Page 528 529 Implementing the Shopping Cart From this page, you can see that we have two other options.There is a Save Changes button that we can use to change the quantity of items in the cart.To do this, you can alter the quantities directly and click Save Changes.This is actually a submit button that takes us back to the show_cart.php script again to update the cart. In addition, there’s a Go to Checkout button that a user can click when she is ready to leave.We’ll come back to that in a minute. For now, let’s look at the code for the show_cart.php script.This code is shown in Listing 25.9. Listing 25.9 show_cart.php—This Script Controls the Shopping Cart <?php include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one session_start(); @ $new = $HTTP_GET_VARS['new']; if($new) { //new item selected if(!isset($HTTP_SESSION_VARS['cart'])) { $HTTP_SESSION_VARS['cart'] = array(); $HTTP_SESSION_VARS['items'] = 0; $HTTP_SESSION_VARS['total_price'] ='0.00'; } if(isset($HTTP_SESSION_VARS['cart'][$new])) $HTTP_SESSION_VARS['cart'][$new]++; else $HTTP_SESSION_VARS['cart'][$new] = 1; $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); } if(isset($HTTP_POST_VARS['save'])) { foreach ($HTTP_SESSION_VARS['cart'] as $isbn => $qty) { if($HTTP_POST_VARS[$isbn]=='0') unset($HTTP_SESSION_VARS['cart'][$isbn]); else $HTTP_SESSION_VARS['cart'][$isbn] = $HTTP_POST_VARS[$isbn]; } $HTTP_SESSION_VARS['total_price'] = 31 525x ch25 1/24/03 3:39 PM Page 529 530 Chapter 25 Building a Shopping Cart calculate_price($HTTP_SESSION_VARS['cart']); $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); } do_html_header('Your shopping cart'); if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])) display_cart($HTTP_SESSION_VARS['cart']); else { echo '<p>There are no items in your cart</p>'; echo '<hr />'; } $target = 'index.php'; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_book_details($new); if($details['catid']) $target = 'show_cat.php?catid='.$details['catid']; } display_button($target, 'continue-shopping', 'Continue Shopping'); // use this if SSL is set up // $path = $HTTP_SERVER_VARS['PHP_SELF']; // $server = $HTTP_SERVER_VARS['SERVER_NAME']; // $path = str_replace('show_cart.php', '', $path); // display_button('https://'.$server.$path.'checkout.php', 'go-to-checkout', 'Go To Checkout'); // if no SSL use below code display_button('checkout.php', 'go-to-checkout', 'Go To Checkout'); do_html_footer(); ?> There are three main parts to this script: displaying the cart, adding items to the cart, and saving changes to the cart.We’ll cover these in the next three sections. Viewing the Cart No matter which page we have come from, we will display the contents of the cart. In the base case, when a user has just clicked View Cart, this is the only part of the code that will be executed, as follows: Listing 25.9 Continued 31 525x ch25 1/24/03 3:39 PM Page 530 531 Implementing the Shopping Cart if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])) display_cart($HTTP_SESSION_VARS['cart']); else { echo '<p>There are no items in your cart</p>'; echo '<hr />'; } As you can see from this code, if we have a cart with some contents, we will call the display_cart() function. If the cart is empty, we’ll give the user a message to that effect. The display_cart() function just prints the contents of the cart as a readable HTML format, as you can see in Figures 25.6 and 25.7.The code for this function can be found in output_fns.php, which is included here as Listing 25.10.Although it is a display function, it is reasonably complex, so we include it here. Listing 25.10 display_cart() Function from output_fns.php—This Function Formats and Prints the Contents of the Shopping Cart function display_cart($cart, $change = true, $images = 1) { // display items in shopping cart // optionally allow changes (true or false) // optionally include images (1 - yes, 0 - no) global $HTTP_SESSION_VARS; echo '<table border="0" width="100%" cellspacing="0"> <form action="show_cart.php" method="post"> <tr><th colspan="'. (1+$images) .'" bgcolor="#cccccc">Item</th> <th bgcolor="#cccccc">Price</th><th bgcolor="#cccccc">Quantity</th> <th bgcolor="#cccccc">Total</th></tr>'; //display each item as a table row foreach ($cart as $isbn => $qty) { $book = get_book_details($isbn); echo '<tr>'; if($images ==true) { echo '<td align="left">'; if (file_exists("images/$isbn.jpg")) { $size = GetImageSize('images/'.$isbn.'.jpg'); if($size[0]>0 && $size[1]>0) { echo '<img src="images/'.$isbn.'.jpg" border=0 '; 31 525x ch25 1/24/03 3:39 PM Page 531 . show_book .php page for this book, PHP and MySQL Web Development. If you look closely at the URL bar, you will see that we have called the script with a parameter this time.The parameter is called new and. display_button('https://'.$server.$path.'checkout .php& apos;, 'go-to-checkout', 'Go To Checkout'); // if no SSL use below code display_button('checkout .php& apos;, 'go-to-checkout', 'Go. code for the show_cart .php script.This code is shown in Listing 25.9. Listing 25.9 show_cart .php This Script Controls the Shopping Cart < ?php include ('book_sc_fns .php& apos;); // The shopping

Ngày đăng: 07/07/2014, 03:20

Mục lục

  • PHP and MySQL Web Development

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

          • Chapter 17: Using Network and Protocol Functions

          • Chapter 18: Managing the Date and Time

          • Chapter 20: Using Session Control in PHP

          • Chapter 21: Other Useful Features

          • Part V: Building Practical PHP and MySQL Projects

            • Chapter 22: Using PHP and MySQL for Large Projects

            • Chapter 24: Building User Authentication and Personalization

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

  • Đang cập nhật ...

Tài liệu liên quan