Templates in the Shipping Module

Example 13-1 and Example 13-2 show a template module that displays the order receipt. This script, called shipping.3, is a replacement for the shipping.2 script discussed in Chapter 12. The output of retrieving Example 13-2 with a Netscape web browser is shown in Figure 13-1. Example 13-1 is the application logic, and Example 13-2 is the template.

Example 13-1. shipping.3 provides an order receipt
  include "xtpl.p";
  include "" ;
  function show_HTML_receipt($custID, $orderID, $connection)
    // Create a new XTemplate object called $xtpl
    $xtpl= new XTemplate ("example.shipping.3.xtpl");
    // Find customer information
    $query = "SELECT *
              FROM customer
              WHERE cust_id = $custID";
    if (!($result = @ mysql_query ($query, $connection)))
       showerror(  );
    // There is only one matching row
    $row = @ mysql_fetch_array($result);
    // Assign the orderId to the template
    $xtpl->assign("ORDER_ID", $orderID);
    // Assign the customer data to the template
    $xtpl->assign("CUSTOMER", $row);
    // Parse the template data
    $orderTotalPrice = 0;
    // list the particulars of each item in the order
    $query = "SELECT  i.qty, w.wine_name, i.price,
                      w.wine_id, w.year, wi.winery_name
              FROM    items i, wine w, winery wi
              WHERE   i.cust_id = $custID
              AND     i.order_id = $orderID
              AND     i.wine_id = w.wine_id
              AND     w.winery_id = wi.winery_id
              ORDER BY item_id";
    if (!($result = @ mysql_query ($query, $connection)))
       showerror(  );
    // Add each item to the email
    while ($row = @ mysql_fetch_array($result))
       // Work out the cost of this line item
       $itemsPrice = $row["qty"] * $row["price"];
       $orderTotalPrice += $itemsPrice;
       $wineDetail = showWine($row["wine_id"], $connection);
       // Assign the qty, wine details, price, and
       // total item cost to the template
       $xtpl->assign("QTY", $row["qty"]);
       $xtpl->assign("WINE", $wineDetail);
              sprintf("%-.2f", $row["price"]));
              sprintf("%-.2f", $itemsPrice));
       // Parse a template row of items
    // Assign the order total to the template
           sprintf("%-.2f", $orderTotalPrice));
    // parse all items
   // parse the whole document
   // output the templated data
  // Main ----------
   // Re-establish the existing session
   session_start(  );
   // Check if the user is logged in
   if (!session_is_registered("loginUsername"))
      $message = "You must login to view your receipt.";
      // Redirect the browser back to the login page
      header("Location: example.order.1.php");
   // Check the correct parameters have been passed
   // unless the script is run correctly
   if (!isset($custID) || !isset($orderID))
      $message = "Incorrect parameters to " .
      header("Location: $HTTP_REFERER");
   // Check this customer matches the custID
   if ($custID != getCustomerID($loginUsername, NULL))
      $message = "You can only view your own receipts!";
      header("Location: example.order.1.php");
   // Open a connection to the DBMS
   if (!($connection = @ mysql_pconnect($hostName,
      showerror(  );
   if (!mysql_select_db($databaseName, $connection))
      showerror(  );
   // Show the confirmation HTML page
   show_HTML_receipt($custID, $orderID, $connection);