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.

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.

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?


Next Previous Contents