function gBuildList$theArray, $type = “ul”{ //given an array of values, builds a list based on that array $temp= “ \n”; foreach $theArray as $value{ $temp .= “ $value \n”; } // end forea
Trang 1Creating the Bottom of the Page
The bottom of the page is very easy I just built some standard page-ending HTML
and added it to thePage.
function buildBottom(){
//builds the bottom of a generic web page
$temp = <<<HERE
</body>
</html>
HERE;
$this->addText($temp);
} // end buildBottom;
Adding Headers and Generic Tags
The tag()method is very useful, because it allows you to surround any text with
any HTML tag (or even an XML tag) you wish The gTag function is similar, but
doesn’t store anything to $thePage To simplify my coding, I wrote gTag() first.
This method creates a temporary variable containing the tag name and contents,
nicely formatted (One of the things I didn’t like about cgi.pmis how horrible the
resulting HTML code looked I want code produced by my programs to look as
good as code produced directly by me.)
The tag() method calls gTag()and adds the results with addText() If I make a
change to the gTag() function, I won’t have to remember to make the same
change in tag() It’s good to avoid rewriting code when you can
//general tag functions
function gTag($tagName, $contents){
//given any tag, surrounds contents with tag
//improve so tag can have attributes
//returns tag but does not add it to page
$temp = “<$tagName>\n”;
$temp = “ “ $contents “\n”;
$temp = “</$tagName>\n”;
return $temp;
} // end tag
function tag($tagName, $contents){
//given any tag, surrounds contents with tag
263
j e
Trang 2//improve so tag can have attributes
$temp = $this->gTag($tagName, $contents);
$this->addText($temp);
} // end tag
//header functions
function h1($stuff){
$this->tag(“h1”, $stuff);
} // end h1
function h2($stuff){
$this->tag(“h2”, $stuff);
} // end h2
function h3($stuff){
$this->tag(“h3”, $stuff);
} // end h3
function h4($stuff){
$this->tag(“h4”, $stuff);
} // end h4
function h5($stuff){
$this->tag(“h5”, $stuff);
} // end h5
function h6($stuff){
$this->tag(“h6”, $stuff);
} // end h6
The h1()through h6()methods are all wrappers around the tag()method that simply provide shortcuts for these very common HTML tags Of course you could add shortcuts for all your other favorite tags
Creating Lists from Arrays
I like the list methods because they really clean up my code The buildList()
methods require two parameters The first is an array, which contains all the ele-ments in the eventual list The second parameter is the list type, without the angle braces The list type defaults to ul, but it could also be olor dl
264
g r
s o
l u
g in
e r
Trang 3The method uses a foreach()loop to step through each element in the array and
then adds an <li></li>pair around the element As usual, the function’s g
ver-sion returns this value to the programmer, and the other verver-sion adds it to
$thePage
function gBuildList($theArray, $type = “ul”){
//given an array of values, builds a list based on that array
$temp= “<$type> \n”;
foreach ($theArray as $value){
$temp = “ <li>$value</li> \n”;
} // end foreach
$temp = “</$type> \n”;
return $temp;
} // end gBuildList
function buildList($theArray, $type = “ul”){
$temp = $this->gBuildList($theArray, $type);
$this->addText($temp);
} // end buildList
Creating Tables from 2-Dimension Arrays
The buildTable() methods work much like the buildList() methods The
gBuildTable()code begins by printing the table header It then creates a foreach
loop to handle the rows Inside the loop it adds the <tr>tag and then opens a
second loop to handle the data array representing each of the row’s cells This
data is encased in <td></td>tags and added to the temporary variable At the end
of the cell loop it is the end of a table row, so the </tr>tag is added to the
tem-porary variable By the time both loops are finished, the function has provided
an HTML table with decent formatting.
function gBuildTable($theArray){
//given a 2D array, builds an HTML table based on that array
$table = “<table border = 1> \n”;
foreach ($theArray as $row){
$table = “<tr> \n”;
foreach ($row as $cell){
$table = “ <td>$cell</td> \n”;
} // end foreach
$table = “</tr> \n”;
} // end foreach
265
j e
Trang 4$table = “</table> \n”;
return $table;
} // end gBuildTable
function buildTable($theArray){
$temp = $this->gBuildTable($theArray);
$this->addText($temp);
} // end buildTable
You might improve this code to allow variables including a table caption, border size, style sheet, and whether the first row or column should be treated as table headers.
Creating Tables One Row at a Time
The other set of table functions allows you to build a table one row at a time The
startTable() method begins the table The $tRow() method builds a table row from an array and accepts a rowTypeparameter EndTable()builds the end-of-table code
function startTable($border = “1”){
$this->thePage = “<table border = \”$border\”>\n”;
} // end startTable
function tRow ($rowData, $rowType = “td”){
//expects an array in rowdata, prints a row of th values
$this->thePage = “<tr> \n”;
foreach ($rowData as $cell){
$this->thePage = “ <$rowType>$cell</$rowType> \n”;
} // end foreach
$this->thePage = “</tr> \n”;
} // end thRow
function endTable(){
$this->thePage = “</table> \n”;
} // end endTable
To be honest, I find the 2D array approach in buildTable()a lot more flexible and powerful than this technique, but I kept it in so you could see an alternative.
266
g r
s o
l u
g in
e r
Trang 5j e
Building Basic Form Objects
The basic form-element methods involve no fancy programming I added the text
that should be printed and allowed appropriate parameters so the user could
cus-tomize the form objects as needed
function gTextbox($name, $value = “”){
// returns but does not print
// an input type = text element
// used if you want to place form elements in a table
$temp = <<<HERE
<input type = “text”
name = “$name”
value = “$value” />
HERE;
return $temp;
} // end textBox
function textbox($name, $value = “”){
$this->addText($this->gTextbox($name, $value));
} // end textBox
function gSubmit($value = “Submit Query”){
// returns but does not print
// an input type = submit element
// used if you want to place form elements in a table
$temp = <<<HERE
<input type = “submit”
value = “$value” />
HERE;
return $temp;
} // end submit
function submit($value = “Submit Query”){
$this->addText($this->gSubmit($value));
} // end submit
You might want to add some similar functions for creating passwords, hidden
fields, and text areas.