"What I'm trying to end up having happen is my CGI script will have popup menus. I was wanting those popup menu's populated with entries from the SQL query. To make this easier I thought I would fetch a hash, dump all of the values from the hash into an array and use that array to in the popup menu."
This, of course, all depends upon what your database table and query results look like. I will assume the following table:
states +--------+------------------+----+ | Field | Type | PK | +--------+------------------+----+ | id | int(10) unsigned | y | | name | varchar(64) | | | abbrev | char(2) | | +--------+------------------+----+And with that, let me introduce HTML::Template and selectall_arrayref with the optional "slice" twist:
But ... we lose stickiness, that is, when you submit the form, the item we selected will no longer be selected. Add this line after the database query (the assignment to $states):use DBI; use HTML::Template; use CGI qw(:standard); my $dbh = DBI->connect( qw(DBI:vendor:database:host user pass), {RaiseError=>1}, ); my $states = $dbh->selectall_arrayref(' select id,name from state order by name ',{Slice => {}}); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(states => $states); print header,$tmpl->output; __DATA__ <form> <select name="states"> <tmpl_loop states> <option value="<tmpl_var id>"><tmpl_var name></option> </tmpl_loop> </select> <p><input type="submit" /></p> </form>
and add a new <tmpl_if> to the option element in the template:$_->{id} == param('state') and $_->{sel} = 1 for @$states;
And you can handle single item selects, but multiple items requires more thought. Sometimes it easier instead to let CGI.pm create the HTML for you:<option value="<tmpl_var id>" <tmpl_if sel>selected="1"</tmpl_if>> <tmpl_var name> </option>
Hope this helps,... beginning same as before ... my $states = $dbh->selectall_arrayref( ... ); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(states => scrolling_list( -name => 'states', -values => [ map $_->{id}, @$states ], -size => 12, -multiple => 'why not', -labels => { map {$_->{id} => $_->{name}} @$states }, ), ); print header,$tmpl->output; __DATA__ <form> <tmpl_var states> <p><input type="submit" /></p> </form>
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to Re: DBI fetchrow_hashref issue
by jeffa
in thread DBI fetchrow_hashref issue
by jorvic
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |