Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

creating CGI::scrolling_list ... can this be shortened?

by geektron (Curate)
on Jul 21, 2005 at 20:23 UTC ( [id://476996]=perlquestion: print w/replies, xml ) Need Help??

geektron has asked for the wisdom of the Perl Monks concerning the following question:

This is quickly becoming my favorite idiom for creating CGI::scrolling_list (and other form elements ), but I'm wondering if it can be shortened any further?

Essentially, I need a sorted-by-name display. It's not a *major* concern ATM, but I can see those 2 hashes getting large eventually ...

my $sth = $dbh->prepare( "SELECT id, display_name FROM membership " + ); my $rawMembers = $dbh->selectall_arrayref( $sth, { Slice => {} } ); my %memberOpts = map { $_->{id} => $_->{display_name} } @$rawMember +s; my %memberLookup = reverse %memberOpts; my $memberWidget = $q->scrolling_list( -name => 'memberships', -values => [ @memberLookup{ sort keys %mem +berLookup } ], -labels => \%memberOpts, -default => [], -multiple => 1, -size => 5, );
After I create these little widgets, they're passed off to HTML_TEMPLATE ... i'm more wondering if anyone sees a way to ditch the second copy of the hash.

Replies are listed 'Best First'.
Re: creating CGI::scrolling_list ... can this be shortened?
by fmerges (Chaplain) on Jul 22, 2005 at 00:11 UTC

    Hi,

    Why you don't change your SQL statement, that way you would get the values in right order, as you firstly wanted without needing to code something ugly and confusing... like:

    my $query = "SELECT id, display_name FROM membership ORDER BY display_ +name"; # use DESC if you don't want ASC order. my $sth = $dbh->prepare($query); ...

    Regards,

    |fire| at irc.freenode.net

    I like merlyn's disclaimer

      Why you don't change your SQL statement, that way you would get the values in right order ...

      because the values may come out of the database in the right order, but the map() used to create the hash destroys the order. (i've tested this, actually.)

        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
Re: creating CGI::scrolling_list ... can this be shortened?
by kutsu (Priest) on Jul 22, 2005 at 21:42 UTC

    Using much the same technique as described here:

    my $sth = $dbh->prepare("SELECT id, display_name FROM membership ORDER + BY display_name"); $sth->execute; my %row; $sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } )); while ($sth->fetch) { my $memberWidget = $q->scrolling_list( -name => '...', -values => [ @row{sort keys %row}], -labels => \%row, ... ); push (@memberWidgets, $memberWidget); }

    I'm not sure about the -values and -labels option of scrolling_list (as I've never used CGI built-ins) but this should get you started (and the keys aren't sorted but the order you get it from the database is the same).

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      well, since you're not familiar with the CGI builtins, you're missing something very imporant ... you're creating N widgets ( where N is the number of rows returned ), not creating N options in the scrolling list.

      i'm sure something more like a Scwartzian Transform in the original  map would do it...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found