<<

. 50
( 132 .)



>>


The next step is to turn the reference to the image file into an HTML tag. So we
pass the array to the get_attlist() function. This takes key/value pairs from an
associative array and reformats them as a single string of HTML-style attributes.
The previous example would come back as the following:

src=”/images/monkey.jpg” alt=”/images/monkey.jpg” border=”0”

Therefore, we only need add the name of the tag itself and the opening and clos-
ing angle brackets to get this, which image_tag() returns as its result:

<image src=”/images/monkey.jpg” alt=”/images/monkey.jpg”
border=”0”>

A special constant, STANDALONE, defined in /functions/basic.php, is useful for
attributes like ˜selected™ in an <option> tag. So

array(˜value™=>™CA™,™selected™=>STANDALONE)

becomes

value=”CA” selected

Using this function may seem like a lot of work just to get a simple <img> tag.
Well, it is. The payoff is flexibility, the cost is an increase in complexity. In a high-
performance environment you would probably end up discarding parts of this code.
For instance, you could decree that all function calls will be of the following form:

my_function(array(˜param1™=>™value1™, ˜param2™=>™value2™, ...)

This would enable you to eliminate the call to parse_arguments() and simply
merge the passed-in array with $_defaults. Or you could use functions like these
in your production/development environment to produce less clever, and thus
faster, files that will then get pushed out to your servers.

FUNCTIONS FROM /BOOK/FUNCTIONS/HTML/
These functions make it easier to create common HTML tags. Most of the functions
in this file are very similar.
282 Part III: Simple Applications

ANCHOR_TAG() This function creates an anchor tag.

function anchor_tag()
{
static $_defaults = array(
˜href™=>™™
, ˜text™ => ˜™
, ˜value™ => ˜™
, ˜allowed™ => array(˜Common™,™accesskey™,™charset™,™href™

,™hreflang™,™rel™,™rev™,™tabindex™,™type™,™name™,™target™
)
);
static $_simple = array(˜href™,™value™);

$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);

if (empty($p[˜text™]))
{
$p[˜text™] = $p[˜href™];
}
if (empty($p[˜value™]))
{
$p[˜value™] = $p[˜text™];
}

$attlist = get_attlist($p);
$output = “<a $attlist>{$p[˜value™]}</a>”;
return $output;
}

You can expect only two things every time with an anchor tag: an href attribute
and some text to go between the opening and closing <a> tags. However, it is pos-
sible that a name attribute would be more descriptive, and more useful in client-side
scripting. But more often than not, the call to this function will look something like
this:

anchor_tag(˜myurl.com/index.html™, ˜this is a great link™);



PARAGRAPH() This function will either print out opening and closing <p> tags
and everything between them, or just the opening <p> tag, depending on how it™s
called.
Chapter 9: Survey 283

function paragraph ()
{
static $_defaults = array(
˜values™ => array()
, ˜allowed™ => array(˜Common™,™align™)
, ˜start™ => NULL
);
static $_simple = array(˜values™);
$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);
$attlist = get_attlist($p);

$output = “\n<p $attlist>\n”;
if ($p[˜start™] !== NULL)
{
return $output;
}
$output .= implode(“\n”,(array)$p[˜values™])
.end_paragraph($p)
;
return $output;
}

The first thing to understand about this function is that by default it will print
not only the opening <p> tag along with its attributes, but also the closing </p> tag
and everything that could occur between the two. This could include anchor tags,
image tags, or just about anything else. The following function call would work just
fine, and in fact is used within the survey application:

print paragraph(anchor_tag(˜block_domain.php™,™Return to Domain
List™));

One argument exists in this function call, and that™s another function call with
two arguments. In effect, when one function call is nested inside another, PHP exe-
cutes the internal one first. So first the anchor_tag() function is called, creating a
string like ˜<a href=”admin_block.php”>™. Then the outer function is executed,
so the call to the paragraph function will actually look something like this:

print paragraph(˜<a href=”admin_block.php”>Return to Domain
List</a>™);

Note how flexible this becomes. By looping through the number of arguments
you can send any number of additional function calls to the paragraph function.
And you can happily mix text and function calls together, because by the time
284 Part III: Simple Applications

paragraph() sees it, it™s all text. So the following is a perfectly fine call to the
paragraph function:

print paragraph(
“<b>Blocked by:</b> $block_by <br>”
, “<b>Date Blocked:</b> $block_dt <br>”
, “<b>Date Released:</b> $release_dt <br>”
, “<b>Last Modified:</b> $modify_dt <br>”

);

START_PARAGRAPH() You might have noticed that the paragraph() function
checked to see if it had been passed an argument named ˜start™, and if it had,
returned only the opening <p> tag. Sometimes you need to use the function that
way because what goes inside the paragraph is too complicated to be included in a
list of values. In such a case you can just call paragraph() with a ˜start™=>TRUE
attribute, or you can use the start_paragraph() function, as follows:

function start_paragraph ()
{
$p = func_get_args();
$p[] = array(˜start™=>™yes™);
return call_user_func_array(˜paragraph™, $p);
}

The start_paragraph() function takes the arguments passed into it and adds a
˜start™ argument. Then comes the interesting part. The PHP function
call_user_func_array () takes a function name and an array of arguments and
uses them to make a call to the named function. The elements in the array of argu-
ments are passed in exactly as they would be in a normal function call. So

call_user_func_array(˜myfunc™,array(1,2,3);

works just like

myfunc(1,2,3);

The call_user_func_array() strategy lets start_paragraph() work as a kind of
front end to the paragraph() function. A call to start_paragraph() like this one:

start_paragraph(array(˜align™=>™center™));

is equivalent to

paragraph(array(˜align™=>™center™, ˜start™=>™yes™));
Chapter 9: Survey 285

Both calls produce the same HTML output:

<p align=”center”>

END_PARAGRAPH() This function just prints out an end paragraph tag (</p>), as
follows:

function end_paragraph ()
{
$output = “\n</p>\n”;
return $output;
}

Its main reason for existing, besides making a lovely matched set with
start_paragraph(), is to let you close any opening tags you might want to hard-
code into the opening of a paragraph ” a <font> tag, for example.

UL_LIST() With this function you can create a bulleted list. Most frequently, an
array will be passed to the function, each element prepended with an <li> tag. The
function also deals with occasions in which a string is sent as the only argument.

function ul_list ()
{
static $_defaults = array(
˜values™ => array()
, ˜contents™ => NULL
, ˜allowed™ => array(˜Common™,™compact™,™type™)
);
static $_simple = array(˜values™);
$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);
$attlist = get_attlist($p);

$output = “<ul $attlist>\n”;

if (!empty($p[˜values™])
&& !is_array($p[˜values™])
&& !is_object($p[˜values™])
)
{
$output .= $p[˜values™];
}
else
{
array_key_remove($p,array(˜_defaults™,™_simple™,™allowed™));
286 Part III: Simple Applications

foreach ((array)$p[˜values™] as $p[˜text™])
{
$output .= li_tag($p);
}
}
$output .= $p[˜contents™];
$output .= “</ul>\n”;
return $output;
}

START_TABLE() Every HTML table begins with more or less the same code, so we
have a function to generate it for us.

function start_table ()
{
static $_defaults = array(
˜cellspacing™ => 0
, ˜cellpadding™ => 1
, ˜allowed™ =>
array(˜Common™,™border™,™cellpadding™,™cellspacing™

,™datapagesize™,™frame™,™rules™,™summary™,™width™,™align™,™bgcolor™
)
);
static $_simple = array(˜width™);
$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);
$attlist = get_attlist($p);
$output = “\n<table $attlist>\n”;
return $output;
}

END_TABLE() The same goes for the end of the table ” it™s boilerplate, and boiler-
plate should be generated programmatically. Here™s a function that does just that.

function end_table ()
{
$output = “\n</table>\n”;
return $output;
}

TABLE() Here, unlike with the similar paragraph functions, start_table() is the
function that knows how to generate the opening <table> tag, and it is the overall

<<

. 50
( 132 .)



>>