Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: CGI's "popup_menu" Using Hash Reference

by fizbin (Chaplain)
on Aug 31, 2005 at 09:22 UTC ( [id://488049]=note: print w/replies, xml ) Need Help??


in reply to CGI's "popup_menu" Using Hash Reference

The posters so far don't seem to have looked up the CGI perldoc before posting. In any case, in order to create a popup menu with labels different from the values, you need to be using both the -values parameter and the -labels parameter. This first of those takes a list reference, and the second a hash reference. So let's take a look at what they require.

As it stands now, you're passing into -values a structure like this:

[ { '1' => { 'Author' => 'Mike', 'idAuthor' => '1' }, '2' => { 'Author' => 'Cecil', 'idAuthor' => '2' } } ]
when what you need to do is pass a structure like this as the -values parameter: [ '1', '2' ], and this as the -labels parameter:
{ '1' => 'Mike', '2' => 'Cecil' }
So the question is, how do you go from one to the other? As with many structure reformulations, the answer is with a clever use of map:
my $values_list = [ sort(keys(%$hash_ref)) ]; my $labels_href = { map {$_->{idAuthor} => $_->{Author}} values(%$hash_ref) }; print $cgi->popup_menu(-name=>'menu_name', -values=>$values_list, -labels=>$labels_href);
If you don't like the order authors are shown in once you have ten or more, change the sort call above so that it sorts numerically.
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://488049]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found