Hi Colette -- 

> I am new with CGI, Perl, the Perl DBI, and mySQL. I am trying 
> to figure out
> the best way to populate a drop down list with values from a 
> table in a
> mySQL database. I suspect it is very easy, but I have not been able to
> pinpoint the appropriate code and get it working as I would expect.

> Could someone show me how this is commonly done with these 
> technologies?


If you're using CGI.pm, you need two different data structures:

  1. An array of IDs
  2. A hash mapping IDs to Names

DBI typically requires some work on your part to convert the results of
a fetch operation into something you could use for CGI.pm's
"popup_menu()" method.  Here's an example:

  my $array_ref = $dbh->selectall_arrayref(
    "select thing_id, thing_name from Things order by thing_name",
    {RaiseError=>1}
  );

  my @popup_ids = ();
  my %popup_labels = ();
  foreach my $thing_ref (@$array_ref) {
    my $thing_id = $thing_ref->[0];
    my $thing_name = $thing_ref->[1];
    push(@popup_ids, $thing_id);
    $popup_labels{$thing_id} = $thing_name;
  }

  my $popup_html = $query->popup_menu(
    -name => 'thing_popup',
    -values => [EMAIL PROTECTED], 
    -labels => \%popup_labels
  );


That ought to work, typos notwithstanding.


Warmest regards,

-Jesse-



--

  Jesse Erlbaum
  The Erlbaum Group
  [EMAIL PROTECTED]
  Phone: 212-684-6161
  Fax: 212-684-6226



Reply via email to