![]() |
![]() |
||
| HOME | WEB DESIGN | COMPUTING | GRAPHICS/PHOTOS | MUSIC | |||
|
Web Sites
Web Design
Web Technology
Panoramic Earth
School Web Sites
Summer Web Sites
DLib PHP Library
Introduction
Download
Installation
Database Examples 1
Database Examples 2
HTML Examples
HtmlPage Examples
DLib PHP Library API
|
![]() |
HTML ExamplesDLib PHP LibraryThe 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) = readContacts ("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 $ref variable and set the 'mode' 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 ();
}
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. 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 otherwise it becomes a hidden field 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. |
|
| Copyright ©2003-2010 davidviner.com Terms and Conditions | |||
![]() | |||