Database Reference
In-Depth Information
my
@acc_def
=
defined
(
$acc_info
->
{
default
})
?
split
(
/,/
,
$acc_info
->
{
default
})
: ();
After splitting the default value, pass the resulting array by reference to the list-
generating function you want to use:
print
checkbox_group
(
-
name
=>
"accessories"
,
-
values
=>
$acc_info
->
{
values
},
-
default
=>
\
@acc_def
,
-
linebreak
=>
1
);
# display buttons vertically
print
scrolling_list
(
-
name
=>
"accessories"
,
-
values
=>
$acc_info
->
{
values
},
-
default
=>
\
@acc_def
,
-
size
=>
3
,
# display 3 items at a time
-
multiple
=>
1
);
# create multiple-pick list
When you use
SET
values like this to create list elements, the values are displayed in the
order they are listed in the column definition. To produce a different display order, sort
the values appropriately.
For Ruby, PHP, and Python, we can create utility functions to generate multiple-pick
items. They have the following invocation syntax:
make_checkbox_group (name, values, labels, default, vertical)
make_scrolling_list (name, values, labels, default, size, multiple)
The
name
,
values
, and
labels
arguments to these functions are similar to those of the
single-pick utility routines described in
Recipe 20.2
.
make_checkbox_group()
takes a
vertical
argument to indicate whether to stack the items vertically rather than hori‐
zontally.
make_scrolling_list()
was already described in
Recipe 20.2
for producing
single-pick lists. To use it here, the
multiple
argument should be true to produce a
multiple-pick list. For both functions, the
default
argument can be an array of multiple
values if several items should be selected initially.
make_checkbox_group()
looks like this (shown here in Ruby; the PHP and Python
versions are similar):
def
make_checkbox_group
(
name
,
values
,
labels
,
default
,
vertical
)
# make sure default is an array (converts a scalar to an array)
default
=
[
default
].
flatten
str
=
""
for
i
in
0
.
.
.
values
.
length
do
# select the item if it corresponds to one of the default values
checked
=
(
default
.
include?
(
values
[
i
]
) ?
" checked=
\"
checked
\"
"
:
""
)
str
<<
sprintf
(
"<input type=
\"
checkbox
\"
name=
\"
%s
\"
value=
\"
%s
\"
%s />%s"
,
CGI
.
escapeHTML
(
name
.
to_s
),
CGI
.
escapeHTML
(
values
[
i
].
to_s
),
checked
,
CGI
.
escapeHTML
(
labels
[
i
].
to_s
))