CART

Linki


» Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.
»
Servadio
»
kiedyś tam zgrał się na giełdzie albo że się za młodu nie nauczył ortografii, albo że zostaje pod pantoflem jejmości dobrodziejki itd...
»
STRING VARIABLE CPEB 15 DS; STRING VARIABLE CPART1 15 DS; ...
»
kamieniem na palcu i niezwykłym instrumentem w ręku, pomyślał, że jeśli ten starzec nie jest dziwny, to znaczy, że dziwnych starców nie ma...
»
li, że wystarczającym oparciem dla państwa może być cnota...
»
— No i nawet nikogo nie trzeba było w tym celu zabijać — potwierdził z po- dziwem Kragar...
»
- Wiem, panie dyrektorze - odpowiedział Rudolf...
»
- Skręcaj, skręcaj! - krzyczał, znając drogę, czując ją w swoich palcach...
»
Na skraju tłumu widać było rękojeść miecza podskakującą jak korek na wodzie, ale nikt nie zwracał uwagi na szermierza, który usiłował interweniować...
»
7

Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.

PHP
Here’s the page that creates our shopping cart.
include “header.php”;
session_register(“cart”);
session_register(“last_item”);
$page_title = anchor_tag(“index.php”,”Bag’O’Stuff”).”:
Shopping Cart”;
include “start_page.php”;
include “cart_form.php”;
include “end_page.php”;
What? Expecting a little more code from your shopping cart? Well, most of it is in the include (cart_form.php). Just note here that the session is started. And that there are two session variables you will be tracking. The cart object, as you will see in a moment, is created with the Order class. Remember that when the two variables are registered on the page, two things happen. First, if they exist already, they are pulled into memory from the session file. Second, if they are changed in the course of the page, they will be written out with those changes at the end of the page.
Note that the $last_item variable holds the description of the last item ordered by the user. We use it to prevent multiple orders of the same item, typically caused by the user hitting the Order! button more than once. If the user wants two of an item instead of one, they can change the quantity for the item.
Now let’s look at the include.
CART_FORM.PHP
As you can see here, if the cart does not exist in the session a new one will be instantiated. There are extensive in-line comments, which should help you get through this script.
3537-4 ch14.f.qc 12/15/00 15:25 Page 389
Chapter 14: Shopping Cart 389 // if $cart is not an Order object, make it one
if (!is_object($cart)) { $cart = new Order; }
// initialize the URL for displaying
// items from the cart with the name
// of the display script, formatted as
// an absolute URL to the regular
// (non-secure) server by the regular_url() function (defined in
// /book/functions/basic.php).
$href = regular_url(“display.php”);
if ($order_item == “Order!”)
{
// create a new CartSubStyle object. as the lowest class in our
// class hierarchy (low == furthest from Base),
// it is an extension
// of all the other classes, and so can access
// all of their methods.
// we will use this to hold any new item to be
// added to the cart.
$t = new CartSubStyle;
// the $last_item variable holds the description of the last
// item ordered by the user. we use it to prevent multiple
// orders of the same item, typically caused by an itchy
// finger on the ‘Order!’ button. if the user wants two of
// an item instead of one, they can change the quantity for
// the item.
// if the cart is empty, of course, there is no previously
// ordered item - set $last_item to an empty string.
if (count($cart->items) == 0) { $last_item = “”; }
// the $item variable will be set to a description of the
// ordered item. initialize it to an empty string.
$item = “”;
if (!empty($product_id))
{
// we at least have a product ID. get information about
// the product category and its category from the database,
// and add links to the category and product to the item
// description.
$t->FetchCategory($category_id);
3537-4 ch14.f.qc 12/15/00 15:25 Page 390
390
Part IV: Not So Simple Applications $href .= “?category_id=$t->category_id”;
$item .= anchor_tag($href,$t->category);
$t->FetchProduct($product_id);
$href .= “&product_id=$t->product_id”;
$item .= “- “.anchor_tag($href,$t->product);
}
if (!empty($style_id))
{
// we have a style ID. get information about the style
// from the database and add the style name to the item
// description. (styles are not individually displayed.)
$t->FetchStyle($style_id);
$item .= “- $t->style”;
}
if (!empty($substyle_id))
{
// we have a substyle ID. get information about the substyle
// from the database and add the substyle name to the item
// description. (substyles are not individually displayed.)
$t->FetchSubStyle($substyle_id);
$item .= “- $t->substyle”;
}
if (!empty($item) && $last_item != $item)
{
// if we have an item description and it is not the
// same as the last item ordered, add the new item
// to the user’s shopping cart.
$cart->AddItem($cart, array(
“item”=>$item
, “product_id” => $product_id
, “style_id” => $style_id
, “substyle_id” => $substyle_id
, “price” => $price
, “quantity” => $quantity
));
}
// set $last_item to the item just ordered (if any)
$last_item = $item;
}
elseif ($again == “please”)
{
// which just means, we’re coming from a submitted cart form,
3537-4 ch14.f.qc 12/15/00 15:25 Page 391
Chapter 14: Shopping Cart 391 // where $again is set to “please” in a hidden field. we test
// this, rather than the value of $submit as in other examples,
// so the user can hit the ENTER key after typing in a new
// quantity or checking remove boxes and get a recalculation,
// without actually pressing the ‘Recalculate’ button.
// for each item in the cart, set its quantity property
// to the corresponding value from the $quantity[] array
// built by PHP from the ‘quantity[$row]’ fields submitted
// from the form.
$quantity = (array)$quantity;
reset($cart->items);
while (list($row,) = each($cart->items))
{
// by adding 0 explicitly, PHP will set the value
// of the quantity property to at least 0, even if,
// for some reason, the user has set the field to
// a blank value.
$cart->items[$row]->quantity = $quantity[$row] + 0;
}
$remove = (array)$remove;
while (list($row,$value) = each($remove))
{
// tag the item for removal by CalculateTotals()

Powered by MyScript