"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:
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>
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):
$_->{id} == param('state') and $_->{sel} = 1 for @$states;
and add a new <tmpl_if> to the option element in the template:
<option value="<tmpl_var id>" <tmpl_if sel>selected="1"</tmpl_if>> <tmpl_var name> </option>
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:
... 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>
Hope this helps,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.