Historia wymaga pasterzy, nie rzeźników.


Default Properties:
◆ none
Methods:
◆ Item. The class constructor. Takes two arguments: $parent and $atts. Calls the Base() method, assigning each element of the $atts array to Object properties.
◆ SaveItem. Takes no arguments, assumes a $this->item_id exists. Will both update existing styles and create new ones as needed.
CLASS CARTCATEGORY
This is the first of the new classes that directly extend the classes created for the catalog application.
Class Name: CartCategory
Extends Category
Default Properties:
◆ none
Methods:
◆ CartCategory. The class constructor. Calls the Base() method, assigning each element of the $atts array to Object properties.
◆ AddProduct. This method overwrites the AddProduct() method that is in the parent (Category) class. It creates an array, called $products, each member of which is an object formed by calling the CartProducts() class.
3537-4 ch14.f.qc 12/15/00 15:25 Page 385
Chapter 14: Shopping Cart 385 In creating the data structure in Chapter 10, you saw how the LoadCategory() method instantiated objects within an array. Instead of having the LoadCategory() class instantiate the object directly, we had LoadCategory() call another method, named AddProduct(), which instantiated the objects. By breaking out AddProduct() into its own separate method, we gained some flexibility, which becomes convenient in this application.
When you instantiate the CartCategory class, the AddProduct() method of this child class overwrites the AddProduct() method of the parent (Category) class. So if you write the following code:
$c = new CartCategory;
$c->LoadProduct($product_id);
you can be sure that the AddProduct() method from the CartCategory call will execute.
Here are the contents of AddProduct() method of the CartCategory class.
function AddProduct($parent,$atts)
{
$this->products[] = new CartProduct($parent,$atts);
}
CLASS CARTPRODUCT
This is similar to the CartCategory class and includes a method for printing Products that is better for the shopping cart.
Class Name: CartProduct
Extends Product
Default Properties:
◆ none
Methods:
◆ CartProduct. The class constructor. Takes two arguments: $parent and $atts.
Calls the Base() method, assigning each element of the $atts array to Object properties.
◆ AddStyle. This method overwrites the AddStyle() method that is in the parent (Product) class. It creates an array, called $styles, each member of which is an object formed by calling the CartStyles() class.
◆ PrintProdct. Takes no attributes. Overwrites the PrintProduct() method of the parent (Product) class.
3537-4 ch14.f.qc 12/15/00 15:25 Page 386
386
Part IV: Not So Simple Applications CLASS CARTSTYLE This is similar to the CartStyle class and includes a method for printing Styles that is better for the shopping cart.
Class Name: CartStyle
Extends Style
Default Properties:
◆ none
Methods:
◆ CartStyle. The class constructor. Takes two arguments: $parent and $atts.
Calls the Base() method, assigning each element of the $atts array to Object properties.
◆ AddSubStyle. This method overwrites the AddSubStyle() method that is in the parent (Style) class. It creates an array, called $substyles, each member of which is an object formed by calling the CartSubStyles() class.
◆ PrintStyleRow. Takes no attributes. Overwrites the PrintStyleRow() method of the parent (Style) class.
CLASS CARTSUBSTYLE
This is similar to the CartSubStyle class and includes a method for printing substyles that is better for the shopping cart.
Class Name: CartSubStyle
Extends SubStyle
Default Properties:
◆ none
Methods:
◆ CartSubStyle. The class constructor. Takes two arguments: $parent and $atts.
Calls the Base() method, assigning each element of the $atts array to Object properties.
◆ AddSubStyle. This method overwrites the AddSubStyle() method that is in the parent (Style) class. It creates an array, called $substyles, each member of which is an object formed by calling the CartSubStyles() class.
◆ PrintSubStyle. Takes three attributes, $style_price, $style_dsc, $product_dsc.
Overwrites the PrintSubStyle() method of the parent (Style) class.
3537-4 ch14.f.qc 12/15/00 15:25 Page 387
Chapter 14: Shopping Cart 387 Scripts
These are the pages called by URLs and the includes. Once again, you will probably notice that there isn’t a whole lot involved. Almost all of the work is done in the Classes.
DISPLAY.PHP
This will print out either a list of categories or a specific product.
include ‘header.php’;
if (empty($category_id))
{
header(“Location: index.php”);
exit;
}
$page_title = anchor_tag(“index.php”, “Bag’O’Stuff”);
$c = new CartCategory;
if (empty($product_id))
{
$c->LoadCategory($category_id);
$page_title .= “: $c->category”;
include “start_page.php”;
$c->PrintCategory();
}
else
{
$p = new CartProduct;
$p->LoadProduct($product_id);
$p->LoadStyles();
$c->FetchCategory($p->category_id);
$page_title .= “: “
.anchor_tag(“display.php?category_id=$c->category_id”
, $c->category
)
.”: $p->product”
;
3537-4 ch14.f.qc 12/15/00 15:25 Page 388
388
Part IV: Not So Simple Applications include “start_page.php”;
$p->PrintProduct();
}
include “end_page.php”;