Database Reference
In-Depth Information
htmlspecialchars
(
$name
),
htmlspecialchars
(
$values
[
$i
]),
$checked
,
htmlspecialchars
(
$labels
[
$i
]));
if
(
$vertical
)
$result
.=
'<br />'
;
# display items vertically
}
return
(
$result
);
}
The function constructs the form element as a string, which it returns. To use
make_ra
dio_group()
to present cow colors, invoke it after fetching the items from the
cow_col
or
table, as follows:
$stmt
=
"SELECT color FROM cow_color ORDER BY color"
;
$sth
=
$dbh
->
query
(
$stmt
);
$values
=
$sth
->
fetchAll
(
PDO
::
FETCH_COLUMN
,
0
);
print
(
make_radio_group
(
"color"
,
$values
,
$values
,
""
,
TRUE
));
Pass the
$values
array to
make_radio_group()
twice because it's used both for the values
and the labels.
To present a pop-up menu, use the following function instead:
function
make_popup_menu
(
$name
,
$values
,
$labels
,
$default
)
{
$result
=
''
;
for
(
$i
=
0
;
$i
<
count
(
$values
);
$i
++
)
{
# select the item if it corresponds to the default value
$checked
=
(
$values
[
$i
]
==
$default
?
' selected="selected"'
:
''
);
$result
.=
sprintf
(
'<option value="%s"%s>%s</option>'
,
htmlspecialchars
(
$values
[
$i
]),
$checked
,
htmlspecialchars
(
$labels
[
$i
]));
}
$result
=
sprintf
(
'<select name="%s">%s</select>'
,
htmlspecialchars
(
$name
),
$result
);
return
(
$result
);
}
make_popup_menu()
has no
$vertical
parameter, but otherwise invoke it the same way
as
make_radio_group()
:
print
(
make_popup_menu
(
"color"
,
$values
,
$values
,
""
));
The
make_scrolling_list()
function is similar to
make_popup_menu()
, so I don't show
its implementation here. To invoke it to produce a single-pick list, pass the same argu‐
ments as for
make_popup_menu()
, but indicate how many rows should be visible at once,
and add a
multiple
argument of
FALSE
: