Next Previous Contents

4. Extended functionality

The section on extended functionality covers non-GUI classes that provide often needed application functions without a user interface. Some extended classes depend on core functionality, some contain independent classes.

Extended classes are treated differently from core classes in that their code is not automatically included by prepend.php3. You have to include the class definition manually where needed or you modify prepend.php3.

4.1 Cart

The Cart class is programmatically independent, but makes sense only if its instances are made persistent in some way. The Cart class automatically registers itself as a session variable in its start() function.

Cart implements a shopping cart. At the moment, items within the shopping cart are independent of each other; the cart can only hold simple things. Support for compound articles that require other articles to function and provide a base for dependent articles is to be added at a future time.

An example of a simple article is any article with no options, for example an apple or a book. Common examples for compound articles are a pizza (which requires a foundation in either American or Italian style, a selection of toppings, and cheese, to function correctly) and a computer system (which requires a housing, a motherboard, RAM, a video card, etc to function correctly).

Note: Cart was a core class up to release-5. If your applications uses the Cart class, you must manually add the statement include("cart.inc") to your prepend.php3 file where indicated in that file.

Note: The page management functions do no longer support the feature cart to set up and start the cart class. It is recommended that you use Session's auto_init feature instead to start your cart automatically or that you manually set up your cart.

Instance variables


classname
Serialization helper: The name of this class.
persistent_slotsSerialization helper: The names of all persistent slots.
itemMultidimensional array of items in the cart.
currentItemA counter for item positions.
Accessible instance variables.

Instance methods

Accessible instance methods

check($art)

Checks that an item with the given article number $art is in the cart. Returns an array of a boolean value and an integer number. If the boolean is true, there are number many articles of that article number in the cart.

reset()

Deletes all items in current cart, resetting $this->currentItem to 1. Always returns true.

tot_arts()

Returns the number of articles in the current shopping cart, or false if cart is empty.

add_item($art, $num)

Add $num many articles of article number $art to the current shopping cart. Returns the position number of $art in the shopping cart.

remove_item

Remove $num many articles of article number $art from the shopping cart, if there are at least that many articles in the cart. Returns the position number of $art in the shopping cart or false, if there weren't enough $art to remove them from the cart. If the function does return false, the cart has not been modified.

set_item

Set the quantity of article number $art in the shopping cart to exactly $num. If $num is set to zero, article is removed from cart. Returns the position number of $art in the shopping cart.

show_all()

Calls show_item_open() once at the beginning of a shopping cart listing. Then calls show_item() once for each item in the shopping cart. Calls show_item_close() once at the end of a shopping cart listing.

show_item($art, $num)

This function should be provided by the user. It renders the HTML to display a single item from the cart. $art is the article number of the item and there are $num of these in the cart.

show_cart_open()

This function should be provided by the user. It renders the prologue HTML to display a shopping cart listing.

show_cart_close()

This function should be provided by the user. It renders the epilogue HTML to display a shopping cart listing.

Example

Use a subclass of Cart to provide an implementation of show_item().


class My_Cart extends Cart {
  var $classname = "My_Cart";

  

Look up article numbers... var $database_class = "DB_Article"; var $database_table = "articles"; var $db;

var $sum = 0;

function show_cart_open() { printf("<table class=cart_table>\n"); $this->sum = 0; }

function show_cart_close() { printf("</table>\n"); printf("That's a total of %s.\n", $this->sum); }

function show_item($art, $num) { if (!is_object($this->db)) { $class = $this->database_class; $this->db = new $class; }

$query = sprintf("select * from %s where artid = '%s'", $this->database_table, $art); $this->db->query($query);

while($this->db->next_record()) { printf(" <tr class=cart_row>\n <td class=cart_cell>%s</td>\n", $this->db->Record["name"]); printf(" <td class=cart_cell>%s</td>\n", $this->db->Record["price"]); printf(" <td class=cart_cell>%s</td>\n", $num); $rowsum = $num * $this->db->Record["price"]; $this->sum += $rowsum; printf(" <td class=cart_cell>%s</td>\n", $rowsum); printf(" </tr>\n"); } } }

To use a cart, create an instance of your Cart subclass and call start(). This will automatically register cart.

It is recommended that you set in your Session subclass in local.inc the slot $auto_init to the value setup.inc and create an include file of that name which contains the following code:


  global $cart;               ## $cart is a global variable.
  $cart = new My_Cart; ## Make a My_Cart instance named $cart
  $cart->start();          ## and have it register itself.

Use add_item() and remove_item to work with your Cart:


  $cart->add_item("101", 2);    ## Add two pieces of "101"
  $cart->remove_item("101", 1); ## Drop one piece of "101"

Use show_all() to display the contents of your cart.


  $cart->show_all();    ## What's in a cart, anyway?

On using Cart

To make use of the Cart class, you need to define a new table in your database that lists all articles you shop should sell. With PHPLIB and MySQL we recommend that you create a new instance of PHPLIB for each virtual web server and a new database for each customer. This database should hold the active_sessions and auth_user tables as well as all application specific tables like for example the article list. In other words, with MySQL we strongly discourage that you use PHPLIB and the MySQL directive use database_name together. There is no support if you do (there is no support if you do as we say, too, because PHPLIB is an open source product you are using on your own risk, but ...).

So let us assume you define a very simple new table articles with a structure like this:


#
# Table structure for table 'articles'
#
CREATE TABLE articles (
  name text,
  price float(8,2),
  artid int(11) DEFAULT '0' NOT NULL auto_increment,
  PRIMARY KEY (artid)
);

This table has an article number called artid, and for each artid there is an article description name and a price. You may extend this minimal definition for your purposes by adding article groups, BLOBs with article images and more, but this will suffice for our example purposes.

Populate this table with some products that suit your taste.

The next step is to teach PHPLIB about the cart class. Three steps are necessary to do so:

Now you have a $cart object available by default on every page that uses PHPLIB. That object is created automatically at session startup, is carried from page to page by PHPLIBs session management and is destroyed by the garbage collection that reaps session records. You do not have to worry anymore about that cart, but simply use it anytime between page_open() and page_close(). PHPLIB does the rest for you.

The Cart class is actually dead stupid. It maintains an array $cart->item[] that holds records about what the user bought. Each $cart->item[$x] consists of a $cart->item[$x]["art"], which is the article number of an item the user wants to buy and of a $cart->item[$x]["num"], which is the # of items with that article number that are wanted. $cart->currentItem is the next $x to use for articles added to $cart->item[].

You add articles to the shopping cart with


$x = $cart->add_item($art, $num)

This will add $num items with the article number $art to your cart contents. If you already have an item with that article number in your cart, the count for that article is increased by $num. Otherwise a new article entry is being created and set to $num. The function does return the $x index into the $cart->item[] array for that article.

To remove an item from the shopping cart, code


$x = $cart->remove_item($art, $num)

This will remove $num items with the article number $art from your cart, if there are that many items in your shopping cart. If you do not have the $art in your cart or there are not $num many $art in your cart, the function will return false and not remove anything from the cart. Otherwise, $num articles with article number $art are taken out of the cart and if the count for that article drops to zero while doing this, we even unset the array element.

You may check how many articles with a given article number are in the cart:


list($have, $num) = $cart->check($art)

The check function does return a two-element array. The first element $have is true, if we have the wanted article in the cart. If $have is true, $num holds the number of articles with that number in the cart, otherwise $num is undefined (actually, it is 0, but you must not rely on that).

Finally, we have a function


$cart->show_all()

which you may call to walk your shopping cart and have Example_Cart to generate a list of articles in your cart. That function will first call $cart->show_cart_open(), for which you may provide code in your subclass. It will then call $cart->show_item($art, $num) for each item in the cart. We have a stupid default implementation for that function in Cart, but you may provide more sophisticated code in Example_Cart for that, too. Finally, at the end of your cart listing, $cart->show_cart_close() is being called, which again may be code of yours.

The example in the previous section shows a more sophisticated implementation of a Cart subclass. That implementation uses show_cart_open() to create an opening table tag (formatted with a CSS class) and sets a counter $cart->sum to zero.

In show_cart_close(), the table is being closed and the $cart->sum counter is printed.

As you might have guessed, show_item($art, $num) queries the database for each article number, retrieves the article description and prices and finally sums up all prices, taking the number of articles per article into consideration. It also generates table rows, printing a nice receipt for the customer.


Next Previous Contents