Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Generating Select elements with foreach loops

by pickledegg (Novice)
on Jul 11, 2006 at 09:30 UTC ( [id://560371]=perlquestion: print w/replies, xml ) Need Help??

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

I have managed to build up a bit of perl to generate a html select element for my form.
It uses static quantities for the list, and compares them with the database hash, if they match then it makes the option tag 'selected'.

If the quantity happens to be 1, I want the name of the option tag to be 'Individual', otherwise the name is the actual number, ie 5,10,25,50 or 100.

Problem is at present, is if the db hash contents are empty, then it will set ALL my option tags to say 'Individual'. See what I am getting at?
@quantities = ('1','5','10','25','50','100'); foreach(@quantities){ print "<option value=\"$_\""; if ($userRecord{$pack_qty}==$_){ print " selected"; } print ">"; if (($userRecord{$pack_qty}==1)){# show name 'individual' for qt +y 1 else show number print "Individual</option>"; }else{ print "$_</option>"; } }
Can someone shine the light of knowledge & wisdom upon my brow?

Replies are listed 'Best First'.
Re: Generating Select elements with foreach loops
by davorg (Chancellor) on Jul 11, 2006 at 09:41 UTC

    This is one area where CGI.pm's HTML generation functions are really good.

    @quantities = ('1','5','10','25','50','100'); my %labels = map { $_ => $_ } @quantities; $labels{1} = 'Individual'; print popup_menu(-name => 'foo', -values => \@quantities, -labels => \%labels, -default => $userRecord{$pack_qty});

    The code above gives the following output:

    <select name="foo" > <option value="1">Individual</option> <option value="5">5</option> <option value="10">10</option> <option value="25">25</option> <option value="50">50</option> <option value="100">100</option> </select>
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thats a nice way of doing it, however that just displays the static menu does it not?
      What about comparing to the db hash and settting the value of selected to suit.
      Sorry I'm not lazy, I've just never really used the CGI module in this way.

        Sorry, yes my example wasn't very clear. If -default is set to one of the values, then the correct item in the list is selected. I think you can get the required value from your %userRecord hash.

        If I change the code to:

        print popup_menu(-name => 'foo', -values => \@quantities, -labels => \%labels, -default => 25);

        Then I get the following output:

        <select name="foo" > <option value="1">Individual</option> <option value="5">5</option> <option value="10">10</option> <option selected="selected" value="25">25</option> <option value="50">50</option> <option value="100">100</option> </select>
        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Generating Select elements with foreach loops
by hv (Prior) on Jul 11, 2006 at 11:21 UTC

    The error is in the check for '1' - you are checking the user record, when you should be checking $_, the quantity for which you are currently generating the option field:

    if ($_==1){# show name 'individual' for qty 1 else show number

    I find this sort of output most easily generated with (s)printf:

    @quantities = ('1','5','10','25','50','100'); printf( '<option value="%s"%s>%s</option>', $_, # option value is the quantity ($_ == $userRecord{$pack_qty}) ? ' selected' : '', ($_ == 1) ? 'Individual' : $_ ) for @quantities;

    Hugo

Log In?
Username:
Password:

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

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

    No recent polls found