Home Web Design Computing Music

DLib Library

The HTML functions available are designed to make the use and output of HTML items such as tables, forms and fields a lot easier. They also attempt to prevent 'bad' HTML in the form of missing closing tags.

Here's an example that expands upon the database ones of the previous pages. It lists all contacts whose last name starts with 'Smi' along with their first address (if any) and their 'type'. However, instead of just printing out the information it displays them using a table:

$types = array ("Family", "Friend", "Acquaintance", "Enemy");

list ($ok, $contactList) = Contact::getRecords ("lastname LIKE 'Smi%'", "lastname");

if ($ok)
{
  pTable (1, "Name", "Address", "Type");

  foreach ($contactList as $contact)
  {
    $addr = new Address ($contact->ref, 1);

    if ($addr->ok)
      $ad = formatAddress ($addr);
    else
      $ad = "No address found";

    tRow (href ($contact->name, "contact.php?ref=$contact->ref&mode=edit"), $ad,
      $types [$contact->ctype]);
  }

  endTable ();
}

The pTable function starts a new preset table of type 1 (setting up tables will be discussed later) and can optionally output table headers - in this case it displays the headings: Name, Address and Type.

Inside the loop we get the first address for each contact we find and use tRow to display each table row. Note that we've wrapped the contact's name in an href function call - this turns it into a hyperlink which, if clicked, will in this case call up a PHP file called contact.php and supply it with the contact's reference in the $contact->ref variable and set the 'mode' parameter to 'edit'.

Finally, endTable closes the table down.

Using a form to collect or display data (optionally for editing) can be done as shown in the following code:

httpParams ("<ref,<mode");
form ();

if ($mode == "edit")
{
  $contact = new Contact ($ref);
  $refField = "<b>$contact->ref<b>" . hidden ("ref", $ref);
}
else
{
  $contact = new Contact ();
  $refField = textInput ("ref", 10);
}

if ($contact->ok)
{
  pTable (1);
  tRow ("Reference", $refFld);
  tRow ("First name", textInput ("firstname", $contact->firstname, 30);
  tRow ("Last name", textInput ("lastname", $contact->lastname, 30);

  $ctype = select ("ctype");

  for ($i = 0; $i < count ($types); $i++)
    $ctype .= option ($i, $types [$i], ($i == $contact->ctype));

  $ctype .= endSelect ();

  tRow ("Type", $ctype);
  tRow ("Date of birth", dateFields ("b", "@", $contact->dob));
  formatRow ("TC2");
  tRow (submit ("Save"));
  endTable ();
  hiddenFields ("mode", "save");
}
else
  print "Error: could not read contact record.";

endForm ();

Several new things have been introduced here. Firstly, let's assume that the code is in the contact.php file (called from the first code example at the top of this page). We need to get hold of the $ref and $mode parameters sent in from the previous page and this is done via the httpParams function - it will generate the required variables for us. Note that the '<' prefix on each item in the comma-separated list sent to httpParams filters out any HTML tags from the parameter data - this gives us a simple way to reduce the possibility that someone may be trying to hack into the site and cause mayhem by sending, say, some JavaScript code.

Next we test the value in $mode to see if we are editing an existing record or adding a new one. If reading an existing record (whose reference will be in $ref) then we read that record. Alternatively, a hidden field is used to store the value of $ref. If it's a new record then we generate a blank Contact record and $refField becomes a text input field named ref.

The textInput function generates an HTML input field of type "text" whose name, value and length are derived from the first three parameters sent to the function. The select, endSelect and option functions assemble a drop-down menu using the HTML input type of "select". The hiddenFields function takes any number of name/value pairs and creates HTML input fields of type "hidden" - in the above example we are setting two hidden fields: mode and ref. See the DLib API pages for more information on these functions.