tailieunhanh - PHP and MySQL Web Development - P112

PHP and MySQL Web Development - P112: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Implementing the Shopping Cart 527 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 . 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 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 variables are called items and total_price respectively. Using the Script Let s begin looking at how the shopping cart code is implemented by looking at the 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 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 . In this case we have clicked the View Cart link when .

TỪ KHÓA LIÊN QUAN