Database Reference
In-Depth Information
values
.
append
(
color
)
cursor
.
close
()
Then convert the list to a form element using one of the following calls:
print
(
make_radio_group
(
'color'
,
values
,
values
,
''
,
True
))
print
(
make_popup_menu
(
'color'
,
values
,
values
,
''
))
print
(
make_scrolling_list
(
'color'
,
values
,
values
,
''
,
3
,
False
))
To present the state list, fetch the names and abbreviations:
values
=
[]
labels
=
[]
stmt
=
"SELECT abbrev, name FROM states ORDER BY name"
cursor
=
conn
.
cursor
()
cursor
.
execute
(
stmt
)
for
(
abbrev
,
name
)
in
cursor
:
values
.
append
(
abbrev
)
labels
.
append
(
name
)
cursor
.
close
()
Then pass them to the appropriate function:
print
(
make_popup_menu
(
'state'
,
values
,
labels
,
''
))
print
(
make_scrolling_list
(
'state'
,
values
,
labels
,
''
,
6
,
False
))
The Ruby and Python utility methods in the
lib
directory do something that their PHP
counterparts do not: explicitly convert to string form all argument values that get in‐
corporated into the list. (You can see this in the Python version of
make_pop
up_menu()
earlier.) This conversion is necessary because the Ruby
CGI.escapeHTML()
and Python
cgi.escape()
methods raise an exception if you pass nonstring values to
them.
We have thus far considered how to fetch rows from the
cow_color
and
states
tables
and convert them to form elements. Another element in the form for the online cow-
ordering application is the field for specifying cow figurine size. The legal values for this
field come from the definition of the
size
column in the
cow_order
table. That column
is an
ENUM
, so getting the legal values for the corresponding form element is a matter of
getting the column definition and parsing it. In other words, use the column metadata
rather than the column data.
As it happens, most of the work for this task has already been done in
Recipe 10.7
, which
develops utility routines to get
ENUM
or
SET
column metadata. In Perl, for example,
invoke the
get_enumorset_info()
function as follows to get the
size
column metadata:
my
$size_info
=
get_enumorset_info
(
$dbh
,
"cookbook"
,
"cow_order"
,
"size"
);
The resulting
$size_info
value is a reference to a hash that has several members, two
of which are relevant to our purposes here: