PHP

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
<?php
  include "xtpl.p";
  include "include.inc" ;
  set_error_handler("errorHandler");
  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
    $xtpl->parse("main.customer");
    $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);
       $xtpl->assign("PRICE",
              sprintf("%-.2f", $row["price"]));
       $xtpl->assign("TOTAL",
              sprintf("%-.2f", $itemsPrice));
       // Parse a template row of items
       $xtpl->parse("main.items.row");
    }
    // Assign the order total to the template
    $xtpl->assign("ORDER_TOTAL",
           sprintf("%-.2f", $orderTotalPrice));
    // parse all items
    $xtpl->parse("main.items");
   // parse the whole document
   $xtpl->parse("main");
   // output the templated data
   $xtpl->out("main");
}
  // Main ----------
   // Re-establish the existing session
   session_start(  );
   // Check if the user is logged in
   if (!session_is_registered("loginUsername"))
   {
      session_register("message");
      $message = "You must login to view your receipt.";
      // Redirect the browser back to the login page
      header("Location: example.order.1.php");
      exit;
   }
   // Check the correct parameters have been passed
   // unless the script is run correctly
   if (!isset($custID) || !isset($orderID))
   {
      session_register("message");
      $message = "Incorrect parameters to " .
                 "example.shipping.3.php";
      header("Location: $HTTP_REFERER");
      exit;
   }
   // Check this customer matches the custID
   if ($custID != getCustomerID($loginUsername, NULL))
   {
      session_register("message");
      $message = "You can only view your own receipts!";
      header("Location: example.order.1.php");
      exit;
   }
   // Open a connection to the DBMS
   if (!($connection = @ mysql_pconnect($hostName,
                                      $username,
                                      $password)))
      showerror(  );
   if (!mysql_select_db($databaseName, $connection))
      showerror(  );
   // Show the confirmation HTML page
   show_HTML_receipt($custID, $orderID, $connection);
?>