1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P112 pptx

5 195 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 198,78 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.. Let’s go ahead and l

Trang 1

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 cartarray

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 itemsand 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.phpscript.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.phpwithout 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.phppage 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 newand has the value 0672317842—that is, the ISBN for the book we have just added to the cart

Trang 2

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.

Trang 3

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.phpscript 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.phpscript.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];

}

Trang 4

$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

Trang 5

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) {

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

TỪ KHÓA LIÊN QUAN