Database Reference
In-Depth Information
$color = htmlspecialchars ( $color );
printf ( '<option value="%s">%s</option>' , $color , $color );
}
print ( "</select> \n " );
Python code to do the same is similar:
stmt = "SELECT color FROM cow_color ORDER BY color"
cursor = conn . cursor ()
cursor . execute ( stmt )
print ( '<select name="color">' )
for ( color , ) in cursor :
color = cgi . escape ( color , 1 )
print ( '<option value=" %s "> %s </option>' % ( color , color ))
print ( '</select>' )
cursor . close ()
The state list requires different values and labels, so the code is slightly more complex.
In PHP, it looks like this:
$stmt = "SELECT abbrev, name FROM states ORDER BY name" ;
$sth = $dbh -> query ( $stmt );
print ( '<select name="state">' );
while ( $row = $sth -> fetch ( PDO :: FETCH_NUM ))
{
$abbrev = htmlspecialchars ( $row [ 0 ]);
$name = htmlspecialchars ( $row [ 1 ]);
printf ( '<option value="%s">%s</option>' , $abbrev , $name );
}
print ( "</select>" );
And in Python, like this:
stmt = "SELECT abbrev, name FROM states ORDER BY name"
cursor = conn . cursor ()
cursor . execute ( stmt )
print ( '<select name="state">' )
for ( abbrev , name ) in cursor :
abbrev = cgi . escape ( abbrev , 1 )
name = cgi . escape ( name , 1 )
print ( '<option value=" %s "> %s </option>' % ( abbrev , name ))
print ( '</select>' )
cursor . close ()
Radio buttons and scrolling lists can be produced in similar fashion. But rather than
doing so, let's use a different approach and construct a set of functions that generate
form elements, given the proper information. The functions return a string representing
the appropriate kind of form element. Invoke them as follows:
make_radio_group (name, values, labels, default, vertical)
make_popup_menu (name, values, labels, default)
make_scrolling_list (name, values, labels, default, size, multiple)
These functions have several arguments in common:
Search WWH ::




Custom Search