<<

. 51
( 132 .)



>>

table() function that calls it. This is because we™d like to be able to pass in the
Chapter 9: Survey 287

width as an argument when we are only opening a table. However, when we™re cre-
ating a whole table, any unlabeled arguments are going to be rows in the resulting
table. Because the two situations need two different values for $_simple,
start_table() can™t be just a front end to table().

function table ()
{
static $_defaults = array(
˜rows™ => array()
);
static $_simple = array(˜rows™);

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

$output = start_table($p);

foreach ((array)$p[˜rows™] as $row)
{
$output .= table_row($row);
}

$output .= end_table($p);

return $output;
}

TABLE_ROW() This function does not only print out the opening <tr> tag and its
attributes; it also prints the table cells that will be nested within the <tr> tags.

function table_row ()
{
static $_defaults = array(
˜cells™ => array()
, ˜allowed™ =>
array(˜Common™,™align™,™valign™,™char™,™charoff™
,™bgcolor™
)
);
static $_simple = array(˜cells™);
$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);
$attlist = get_attlist($p);
$output = “\n <tr $attlist>\n”;
288 Part III: Simple Applications

foreach ((array)$p[˜cells™] as $cell)
{
if (!preg_match(˜/<t[dh]/i™, $cell))
{
$output .= table_cell($cell);
}
else
{
$output .= $cell;
}
}
$output .= “\n </tr>\n”;
return $output;
}

The following table_row() call has two arguments, one of which is itself
another function call. The table_cell() function (described later) is executed first,
and the results are passed in to table_row().

print table_row(
˜<b>A simple cell</b>™
, table_cell(array(˜value™=>™A not-so-simple cell™,
˜align™=>™right™))
);

So when table_row() goes through the values in its $cells argument, it finds
one plain string (˜<b>A simple cell</b>™), which it runs through table_cell()
itself, and one already-formatted cell (the output of the table_cell() call in our
initial code), which it just tacks onto its output string as is.

TABLE_CELL() Not too much is new here. It might be worth pointing out the way
the $value attribute is handled: You check to see if it™s an array or an object,
because PHP lets you cast an object as an array ” you get back an associative array
of the properties of the object.

function table_cell ()
{
static $_defaults = array(
˜align™ => ˜left™
, ˜valign™ => ˜top™
, ˜value™ => ˜™
, ˜allowed™ =>
array(˜Common™,™abbr™,™align™,™axis™,™char™,™charoff™

,™colspan™,™headers™,™rowspan™,™scope™,™valign™,™width™,™height™
Chapter 9: Survey 289

,™nowrap™,™bgcolor™
)
);
static $_simple = array(˜value™);

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

if (is_array($p[˜value™]) or is_object($p[˜value™]))
{
$p[˜value™] = implode(˜™,(array)$p[˜value™]);
}

$output = “\n <td $attlist>{$p[˜value™]}</td>\n”;
return $output;
}

FUNCTIONS FROM /BOOK/FUNCTIONS/FORMS.PHP
Most of these functions are fairly straightforward and don™t require any explana-
tion. We will show a couple just for examples.

text_field() This prints out a text field. All the expected attributes should be
passed to the function. (Note: labelize() is a function in /book/functions/basic ”
essentially a slightly trickier version of ucwords().)

function text_field ()
{
static $_defaults = array(
˜type™ => ˜text™
, ˜size™ => 40
, ˜name™ => ˜textfield™
, ˜label™ => NULL
, ˜default™ => NULL
, ˜value™ => NULL
, ˜source™ => NULL
);
static $_simple = array(˜name™,™label™,™default™);
$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);
array_key_remove($p,array(˜_defaults™,™_simple™));
if ($p[˜label™] === NULL)
{
$p[˜label™] = labelize($p[˜name™]);
}
290 Part III: Simple Applications

$p[˜value™] =
get_field_value($p[˜name™],$p[˜default™],$p[˜value™],$p[˜source™]);
return input_field($p);
}

Most of the other functions look similar to this one, the only real exceptions
being the checkbox and radio button.

checkbox_field() The only thing that may be of interest about this function is how
we decide if a checkbox is to be checked by default. We can do this by adding an
argument called $match. If $match equals either the value of the field or the label
(unless you tell it not to match the label by setting label_match to FALSE), the field
will be checked when displayed. The radio_field() function works the same way.

function checkbox_field ()
{
static $_defaults = array(
˜type™ => ˜checkbox™
, ˜name™ => ˜checkboxfield™
, ˜value™ => ˜™
, ˜label™ => NULL
, ˜match™ => NULL
, ˜default™ => NULL
, ˜checked™ => NULL
, ˜source™ => NULL
, ˜prefix™ => ˜<nobr>™
, ˜suffix™ => ˜</nobr>™
, ˜label_match™ => TRUE
);
static $_simple = array(˜name™,™value™,™label™);

$p = func_get_args();
$p = parse_arguments($p, $_simple, $_defaults);
if ($p[˜label™] === NULL)
{
$p[˜label™] = labelize($p[˜value™]);
}

if (!$p[˜skip_selection™])
{
$p[˜value™] = get_field_value( $p[˜name™]
, $p[˜default™]
, $p[˜value™]
, $p[˜source™]
);
Chapter 9: Survey 291

$p[˜checked™] = (
in_array($p[˜value™],(array)$p[˜match™])
|| (
$p[˜label_match™]
&& in_array($p[˜label™],(array)$p[˜match™])
)
) ? STANDALONE : NULL ;
}
$output = $p[˜prefix™].input_field($p).™
˜.$p[˜label™].$p[˜suffix™];

return $output;
}

FUNCTIONS AND CODE FROM /BOOK/BOOK.PHP
This is a kind of uberheader file, which the following examples include to set up the
basic environment and call in the reusable functions from /book/functions.

book_constants() We store information about how your site is configured in a file
named ˜book.ini™, using the same format as PHP™s own ˜php.ini™ file. This lets us use
the built-in function parse_ini_file() to read it in and set up the location of
your /book directory, your /dsn directory, etc. as constants.

function book_constants()
{
static $constants = NULL;
if ($constants === NULL)
{
$ini_file = dirname(__FILE__).™/book.ini™;
if (!file_exists($ini_file))
{
generate_ini_file($ini_file);
}
$constants = parse_ini_file($ini_file);
foreach ($constants as $k => $v)
{
if (!defined($k))
{
define($k, $v);
}
}
}
return $constants;
}
292 Part III: Simple Applications

path_separator() This is a simple function to figure out what character separates
directory names for your environment:

function path_separator()
{
static $path_separator = NULL;
if ($path_separator === NULL)
{
// if the include path has semicolons in it at all, then
they™re
// there to separate the paths; use a colon otherwise
if (strchr(ini_get(˜include_path™),™;™) === FALSE)
{
$path_separator = ˜:™;
}
else
{
$path_separator = ˜;™;
}
}
return $path_separator;
}

add_to_include_path() This function adds a directory to PHP™s default include path.

function add_to_include_path()
{
$include_path = ini_get(˜include_path™);



$ps = path_separator();
book_constants();

<<

. 51
( 132 .)



>>