Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Drop Down Year List

by tedspinkle (Novice)
on Aug 20, 2009 at 23:58 UTC ( [id://790210]=perlquestion: print w/replies, xml ) Need Help??

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

Hello All,

I'm trying to add a drop down menu in my Perl shopping cart to select the credit card expiration year during checkout, but I'm having a hard time with Perl. I'm trying to generate an option list showing the next 9 years. Here's what I have so far:

($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek +, $dayOfYear, $daylightSavings) = localtime(); $year = 1900 + $yearOffset; print '<select name="year">'; for ($year ; $year < $year + 9 ; $year++) { print "<option value=\"$year\">$year</option>\n"; } print '</select>';

Would someone be willing to help me with this? I'm sure this code is not correct. Also, how would you display the menu on an HTML page? I realize that this is over my head, but I'd really like to do this.

Thanks

Replies are listed 'Best First'.
Re: Drop Down Year List
by JavaFan (Canon) on Aug 21, 2009 at 00:21 UTC
    $year will always be less than $year + 9, so your loop will not terminate. A proper way to write the loop is:
    for (my $y = $year; $y <= $year + 9; $y++) { print "<option value = '$y'>$y</option>\n"; }
    Of course someone will come around and show how l44+ he is because he knows another way to write the loop.

    Oh, and as for showing it in an HTML page - I would have the user type in the (2 digit) year. Typing 2 characters is faster than using the mouse.

      As long as you're looking for 1733+ code, I'll add this one. I'm not intending to slight your code - it looks fine. It also is very reminiscent of C (and thus C++, Java, etc.). Here is one that is more perlish insofaras it's more unique to Perl:

      for my $y ($year .. $year + 8) { # this is inclusive print "<option value = '$y'>$y</option>\n"; }
      Or, even more perlishly:
      print "<option value = '$_'>$_</option>\n" for $year .. $year + 8;
      I prefer the first one in this case - the print is too long, forcing the for off the side of the screen or, as I did, on to the next line. IMO, it breaks the flow. I often use these modifiers at the end of the line, but just feel this one is less desirable.

      As to typing the year: I understand why some people prefer the drop-down. But, for me, I wish those people would shut up. I'd prefer the typing, too.

Re: Drop Down Year List
by bichonfrise74 (Vicar) on Aug 21, 2009 at 00:29 UTC
    I'm not sure if this is what you want...
    #!/usr/bin/perl use strict; my $year = 1900 + (localtime)[5]; print qq{<select name="year">\n}; for ( my $i = $year; $i <= $year + 9; $i++ ) { print qq{<option value="$i">$i</option>\n}; } print qq{</select>};
    Output:
    <select name="year"> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> </select>

      Thanks to both of you for the super fast response! The page that I want to display the option list on is not a Perl page. It's an HTML page with proprietary perl tags for the cart. So could this code be converted to a function or something and called on that page with a tag like <! year>?

      I apologize that my Perl is still so bad. =:\

Re: Drop Down Year List
by imrags (Monk) on Aug 21, 2009 at 09:12 UTC
    I have done something similar in a html table (using CGI)
    Here's the part of code, hope it helps:
    ##########to set date/year code ############ @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun); ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek +, $dayOfYear, $daylightSavings) = localtime(); $year = 1900 + $yearOffset; my @years = ($year, $year+1,$year+2); ###################################################### ########To have the years, and use them############### my @years = (2006,2007,2008,2009); my $query = new CGI; ####Other cgi code#### print $query->Tr( $query->popup_menu( -name => 'syear', -values => \@years, -default => $eyear, -labels => @years, ) );
    Depending on where you have the problem, you can use the code.
    Raghu
Re: Drop Down Year List
by Anonymous Monk on Aug 21, 2009 at 06:23 UTC
    hi friend, I am sure of that, the following code will be working fine.
    #!/usr/bin/perl print "Content-type:text/html\n\n"; ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek +, $dayOfYear, $daylightSavings) = localtime(); $year = 1900 + $yearOffset; $i=$year; $i1=$i+9; print '<select name="year">'; for ($i ; $i <= $i1; $i++) { print "<option value=\"$i\">$i</option>\n"; } print '</select>';
Re: Drop Down Year List
by tedspinkle (Novice) on Aug 21, 2009 at 15:44 UTC

    I've got the code working on a page that's generated by perl, but I'm still stumped on how to show it on one of the cart pages that's not generated by perl. This cart uses a tag system that I can't decipher.

    Do I need to use some sort of templating system for this, or is there a way to somehow embed perl results in an HTML page?

    Any pointers would be greatly appreciated.

    Thanks again

      Perhaps we need some of the bigger picture? If the page you want to edit is static then you are stuffed from the get go unless you can change to generating it dynamically. If it is not static then something generates it, but we don't know what the something is and we don't know what resources the page is generated from.

      It often helps to sketch the bigger picture right at the start so we have a context to fit the question into to provide better focused answers.


      True laziness is hard work
Re: Drop Down Year List
by tedspinkle (Novice) on Oct 10, 2009 at 23:00 UTC

    Well I finally got this to work. My problem was that, instead of "printing" the menu, I needed to "return" it and then call it with a placeholder on the html page (I apologize if that's not the proper terminology). Here's what I have:

    sub make_year_range_html { my $year = 1900 + (localtime)[5]; my $html = qq{<select name="CC_ExpDate_Year">\n}; $html .= qq{<option value="" selected>--</option>\n}; for my $ccy ($year .. $year + 9) { $html .= qq{<option value="$ccy">$ccy</option>\n}; } $html .= qq{</select>\n}; return $html; }

    Next, I created the placeholder with this code:

    $main::global->{form}->{year_range} = make_year_range_html();

    Then I put the placeholder on the html page, like so:

    <! year_range>

    It works perfectly! Thanks to everyone who helped me with this. This has taught me a lot about how Perl works in a cgi environment, especially how to create sub routines and using the output with placeholders. Exciting stuff!

    Ted

Log In?
Username:
Password:

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

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

    No recent polls found