<<

. 84
( 132 .)



>>


print start_page(
anchor_tag(˜index.php™, “Bag™O™Stuff”)
. ˜<br>&nbsp;<br>Check Out™
);

print paragraph(
start_form()
, submit_field(array(˜name™=>™reset_order™,™value™=>™Reset Order
Object™))
, end_form()
);

// include the shopping cart form
print print_cart($order);

// begin the order form.
print start_form();

print hidden_field(˜sessid™, session_id());



print subtitle(˜User Info™);
// store the IDs of the user and address (if any)
print hidden_field(˜user_id™, $order->user_id);
498 Part IV: Not So Simple Applications

print hidden_field(˜address_id™, $order->address_id);

start_table(array(˜_defaults™=>array(˜cellspacing™=>4)));

print start_table();

// display the user™s email address, along with the button they
// can use to ask to check the database for address information.
print labeled_row(
˜Email:™
, text_field(array(
˜name™=>™email™
, ˜value™=>$order->email
, ˜size™=>20
))
. ˜&nbsp;™
. submit_field(˜Get My Info™)
);

print labeled_row(
˜First Name:™
, text_field(array(
˜name™ => ˜firstname™
, ˜value™ => $order->firstname
, ˜size™ => 42
, ˜maxlength™ => 40
))
);
print labeled_row(
˜Last Name:™
, text_field(array(
˜name™ => ˜lastname™
, ˜value™ => $order->lastname
, ˜size™ => 42
, ˜maxlength™ => 40
))
);

print table_row(table_cell(array(˜colspan™=>2, ˜value™=>™&nbsp;™)));

// these fields contain any address information that might have been
// directly entered by the user before the database was searched, or
// the information from an address from the database that has been
// selected by the user. in any case, *these* fields are what will
Chapter 14: Shopping Cart 499

// be used in the order.

print labeled_row(
˜Address:™
, text_field(array(
˜name™ => ˜address1™
, ˜value™ => $order->address1.™-new™
, ˜size™ => 42
, ˜maxlength™ => 40
))
);
print labeled_row(
˜&nbsp;™
, text_field(array(
˜name™ => ˜address2™
, ˜value™ => $order->address2
, ˜size™ => 42
, ˜maxlength™ => 40
))
);
print labeled_row(
˜City:™
, text_field(array(
˜name™ => ˜city™
, ˜value™ => $order->city
, ˜size™ => 42
, ˜maxlength™ => 40
))
);
print labeled_row(
˜State:™
, select_field(array(
˜name™ => ˜state™
, ˜values™ => states()
, ˜match™ => $order->state
))
);
print labeled_row(
˜Zip:™
, text_field(array(
˜name™ => ˜zip™
, ˜value™ => $order->zip
, ˜size™ => 12
, ˜maxlength™ => 10
500 Part IV: Not So Simple Applications

))
);
print labeled_row(
˜Phone:™
, text_field(array(
˜name™ => ˜phone™
, ˜value™ => $order->phone
, ˜size™ => 22
, ˜maxlength™ => 20
))
);

if ($order->address_id)
{
// allow the user to create a new address
print table_row(
˜&nbsp;™
, checkbox_field(array(
˜name™ => ˜save_as_new™
, ˜value™ => ˜yes™
, ˜label™ => ˜Save this as a new address™
))
);
}

print end_table();

// display the available shipping methods
print subtitle(˜Shipping Info™);

print start_table(array(˜cellspacing™=>4));

print table_row(
table_header_cell(˜Shipping Method™)
, table_header_cell(array(˜value™=>™Per Order™,
˜align™=>™right™))
, table_header_cell(array(˜value™=>™Per Item™,
˜align™=>™right™))
, table_header_cell(array(˜value™=>™Total for Order™,
˜align™=>™right™))
);

// set up shipping methods

foreach ($order->shipping_methods() as $shipping_id => $r)
Chapter 14: Shopping Cart 501

{
// calculate the cost of using this method. we use a simplistic
// system: a fixed cost per order, and a per item charge.
$shiptotal = $r[˜per_order™] + ($order->total_quantity *
$r[˜per_item™]);

// display the shipping method with a radio field allowing the
// user to choose it
print table_row(
radio_field(array(
˜name™ => ˜shipping_id™
, ˜value™ => $shipping_id
, ˜label™ => $r[˜shipping™]
, ˜match™ => $order->shipping_id
))
, table_cell(array(˜value™=>money($r[˜per_order™]),
˜align™=>™right™))
, table_cell(array(˜value™=>money($r[˜per_item™]),
˜align™=>™right™))
, table_cell(array(˜value™=>money($shiptotal),
˜align™=>™right™))
);
}
print end_table();

// display payment information
print subtitle(˜Credit Card Info™);

print start_table();

if ($order->error)
{
// if the user tried to place an order and there was an error
// when validating or charging the card, display it here.
print table_row(
table_cell(array(
˜style™ => ˜color:red™
, ˜value™ => $order->error
, ˜colspan™=>2
))
);
$order->error = ˜™;
}

// display a test card number in the form for this example by
default.
502 Part IV: Not So Simple Applications

// it has a valid format, and since we™re not really trying
// to charge any cards here, AuthorizeNet will accept it.
if (empty($order->cc_number))
{
$order->cc_number = ˜4912-7398-07156™;
}

// pick Visa as the default type, to match the default test card
number
if (empty($order->cc_type_code))
{
$order->cc_type_code = ˜vis™;
}

// display the accepted credit card types as radio button fields
$cc_types = $order->dbh()->getAssoc(˜select cc_type_code, cc_type
from cc_types™);

print labeled_row(
˜Credit Card:™
, select_field(array(
˜name™ => ˜cc_type_code™
, ˜match™ => $order->cc_type_code
, ˜values™ => $cc_types
))
);

print labeled_row(
˜Number:™
, text_field(array(
˜name™=>™cc_number™
, ˜value™=>$order->cc_number
, ˜size™=>22
))
);

// set the variables used to enter the credit card expiration date

// set the $months array to a list of possible months
$months = array(
1 => 1
, 2 => 2
, 3 => 3
, 4 => 4
, 5 => 5
Chapter 14: Shopping Cart 503

, 6 => 6
, 7 => 7
, 8 => 8
, 9 => 9
, 10 => 10
, 11 => 11
, 12 => 12
);

<<

. 84
( 132 .)



>>