in reply to Re^2: creating CGI::scrolling_list ... can this be shortened?
in thread creating CGI::scrolling_list ... can this be shortened?

Hi again,

What I don't understand is why you are using a hash for this, it's not necessary, you can use the result array, it gives you the right order... taking the result and then copying it to a hash is not necessary.

And if you want to do it with hashes, because you want to, you can use sorted hashes or use a complexe sort method...

But really, don't understand why you need a hash in this case.

Regards,

|fire| at irc.freenode.net
  • Comment on Re^3: creating CGI::scrolling_list ... can this be shortened?

Replies are listed 'Best First'.
Re^4: creating CGI::scrolling_list ... can this be shortened?
by geektron (Curate) on Jul 25, 2005 at 16:52 UTC
    What I don't understand is why you are using a hash for this, it's not necessary, you can use the result array, it gives you the right order... taking the result and then copying it to a hash is not necessary.

    well, in order to have the labels for the scrolling_llist different than the values, per the CGI documentation, an array of values and a hash of value -> label mappings *is* needed.

    i'm creating:

    <select> <option value='1'>Foo </option> <option value='3'>Baz </option> </select>
    now i realize that hashes are nothing more than special arrays, *but* in order to create the sample above, CGI::scrolling_list requires an array of values *and* the (optional) hash of label mappings.

    i haven't tried to let perl massage the resulting array into a hash, *but* it's also an arrayref of hashrefs (as i've written the fetch), and CGI::scrolling_list doesn't understand that.

      Hi,

      Try this:

      #... use strict; use DBI; use CGI; #... my $statement = "SELECT id, display_name FROM membership ORDER BY disp +lay_name"; my $result = $dbh->selectall_arrayref($statement); my %h = map { $_->[0] => $_->[1] } @$result; my $cgi = new CGI; print $cgi->scrolling_list( -name => 'list_name', -multiple => 'true', -values => [ sort keys %h ], -labels=> \%h, ); #...

      Read the man page of DBI, there is explain how the combination of -values and -labels work.

      And don't forget to check the result of the statement.

      Regards,

      |fire| at irc.freenode.net