MySQL Queries with PHP
CSCI 2910-001 In-Class Exercise

Using HTML_Template_IT

The exercise we did for our introduction to PHP had you create a table using PHP. The table should have looked like the following:

I 1/I I2 I! 2I I0.5
1 1 1 1 2 1
2 0.5 4 2 4 1.4142135623731
3 0.33333333333333 9 6 8 1.7320508075689
4 0.25 16 24 16 2
5 0.2 25 120 32 2.2360679774998
6 0.16666666666667 36 720 64 2.4494897427832
7 0.14285714285714 49 5040 128 2.6457513110646
8 0.125 64 40320 256 2.8284271247462
9 0.11111111111111 81 362880 512 3
10 0.1 100 3628800 1024 3.1622776601684

Today's exercise asks you to do the same thing except that you will be using PEAR's HTML_Template_IT library that we discussed in class last week.

Remember that an HTML_Template_IT template consists of HTML code with tags identifying blocks and tags identifying placeholders. Below I've created an HTML_Template_IT template that creates a six-column table with headings. Copy it and past it into a file that you will store as table_6col.tpl.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Generating Date Dropdown Menu with HTML_Template_IT</title>
</head>
<body>
<table align="center" border="2" cellpadding="5">

<!-- BEGIN TABLE_HEADING -->
    <tr>
    <td>{column1}</td>
    <td>{column2}</td>
    <td>{column3}</td>
    <td>{column4}</td>
    <td>{column5}</td>
    <td>{column6}</td>
    </tr>
<!-- END TABLE_HEADING -->

<!-- BEGIN TABLE_DATA -->
    <tr>
    <td>{column1}</td>
    <td>{column2}</td>
    <td>{column3}</td>
    <td>{column4}</td>
    <td>{column5}</td>
    <td>{column6}</td>
    </tr>
<!-- END TABLE_DATA -->

</table>
</body>

</html>

Your PHP file should not have any HTML code in it. The HTML_Template_IT template code should provide all of the HTML.

The first step in using a template is to add the code to provide access to the template itself. This involves including the library, creating an instance of the template object to provide access to the template functions, and loading the template file. The following code takes care of that. (Note that the code below assumes that your template is located in a subdirectory called template_folder below the current directory, i.e., the directory that the PHP script is stored in.)

<?php
// Make template accessible by this PHP script
    require_once ("/usr/local/lib/php/HTML/Template/IT.php");
    $template = new HTML_Template_IT("./template_folder");
    $template->loadTemplatefile("table_6col.tpl", true, true);

Next, we need to select a block in which to insert data. This is done with the HTML_Template_IT function setCurrentBlock(). For example, if we were to write the PHP code to insert data into the placeholders of the block TABLE_HEADING, we would select the block with the following code:

$template->setCurrentBlock("TABLE_HEADING");

Now that we've selected a block, we need to insert data into the placeholders. This is done using the HTML_Template_IT function setVariable(). For example, if we were to write the PHP code to insert the string "I" into the placeholder column1 of the block TABLE_HEADING (the selected block), we would use the following code:

$template->setVariable("column1", "I");

Once you are finished setting the values of a block, it can be parsed or processed using the HTML_Template_IT function parseCurrentBlock(). $template->parseCurrentBlock();

After you have finished processing all of the blocks, the page must be output. This is done using the HTML_Template_IT function show(). $template->show();

The following code outputs the headings of the table shown above.

<?php
// Make template accessible by this PHP script
    require_once ("/usr/local/lib/php/HTML/Template/IT.php");
    $template = new HTML_Template_IT("./template_folder");
    $template->loadTemplatefile("table_6col.tpl", true, true);

    $template->setCurrentBlock("TABLE_HEADING");
    $template->setVariable("column1", "I");
    $template->setVariable("column2", "1/I");
    $template->setVariable("column3", "I<sup>2</sup>");
    $template->setVariable("column4", "I!");
    $template->setVariable("column5", "2<sup>I</sup>");
    $template->setVariable("column6", "I<sup>0.5</sup>");
    $template->parseCurrentBlock();
    $template->show();
?>

Now it is your job to convert the loop you created in the introductory PHP exercise so that it will use the HTML_Template_IT template for creating a six column table shown above. To do this, replace the code inside of the loop with things:

Things to look out for:

Good luck!