#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Over The Rainbow
    Posts
    0
    Rep Power
    0

    How do I shorten marking select boxes "selected"


    Hi


    I am trying to figure a way to shorten this!


    Code:
    if ($on_day eq "01") {
    $dd_1 = " selected";
    }
    if ($on_day eq "02") {
    $dd_2 = " selected";
    }
    if ($on_day eq "03") {
    $dd_3 = " selected";
    }
    if ($on_day eq "04") {
    $dd_4 = " selected";
    }
    if ($on_day eq "05") {
    $dd_5 = " selected";
    }
    if ($on_day eq "06") {
    $dd_6 = " selected";
    }
    if ($on_day eq "07") {
    $dd_7 = " selected";
    }
    if ($on_day eq "08") {
    $dd_8 = " selected";
    }
    if ($on_day eq "09") {
    $dd_9 = " selected";
    }
    if ($on_day eq "10") {
    $dd_10 = " selected";
    }
    if ($on_day eq "11") {
    $dd_11 = " selected";
    }
    if ($on_day eq "12") {
    $dd_12 = " selected";
    }
    if ($on_day eq "13") {
    $dd_13 = " selected";
    }
    if ($on_day eq "14") {
    $dd_14 = " selected";
    }
    if ($on_day eq "15") {
    $dd_15 = " selected";
    }
    if ($on_day eq "16") {
    $dd_16 = " selected";
    }
    if ($on_day eq "17") {
    $dd_17 = " selected";
    }
    if ($on_day eq "18") {
    $dd_18 = " selected";
    }
    if ($on_day eq "19") {
    $dd_19 = " selected";
    }
    if ($on_day eq "20") {
    $dd_20 = " selected";
    }
    if ($on_day eq "21") {
    $dd_21 = " selected";
    }
    if ($on_day eq "22") {
    $dd_22 = " selected";
    }
    if ($on_day eq "23") {
    $dd_23 = " selected";
    }
    if ($on_day eq "24") {
    $dd_24 = " selected";
    }
    if ($on_day eq "25") {
    $dd_25 = " selected";
    }
    if ($on_day eq "26") {
    $dd_26 = " selected";
    }
    if ($on_day eq "27") {
    $dd_27 = " selected";
    }
    if ($on_day eq "28") {
    $dd_28 = " selected";
    }
    if ($on_day eq "29") {
    $dd_29 = " selected";
    }
    if ($on_day eq "30") {
    $dd_30 = " selected";
    }
    if ($on_day eq "31") {
    $dd_31 = " selected";
    }
    if ($of_day eq "01") {
    $pp_1 = " selected";
    }
    if ($of_day eq "02") {
    $pp_2 = " selected";
    }
    if ($of_day eq "03") {
    $pp_3 = " selected";
    }
    if ($of_day eq "04") {
    $pp_4 = " selected";
    }
    if ($of_day eq "05") {
    $pp_5 = " selected";
    }
    if ($of_day eq "06") {
    $pp_6 = " selected";
    }
    if ($of_day eq "07") {
    $pp_7 = " selected";
    }
    if ($of_day eq "08") {
    $pp_8 = " selected";
    }
    if ($of_day eq "09") {
    $pp_9 = " selected";
    }
    if ($of_day eq "10") {
    $pp_10 = " selected";
    }
    if ($of_day eq "11") {
    $pp_11 = " selected";
    }
    if ($of_day eq "12") {
    $pp_12 = " selected";
    }
    if ($of_day eq "13") {
    $pp_13 = " selected";
    }
    if ($of_day eq "14") {
    $pp_14 = " selected";
    }
    if ($of_day eq "15") {
    $pp_15 = " selected";
    }
    if ($of_day eq "16") {
    $pp_16 = " selected";
    }
    if ($of_day eq "17") {
    $pp_17 = " selected";
    }
    if ($of_day eq "18") {
    $pp_18 = " selected";
    }
    if ($of_day eq "19") {
    $pp_19 = " selected";
    }
    if ($of_day eq "20") {
    $pp_20 = " selected";
    }
    if ($of_day eq "21") {
    $pp_21 = " selected";
    }
    if ($of_day eq "22") {
    $pp_22 = " selected";
    }
    if ($of_day eq "23") {
    $pp_23 = " selected";
    }
    if ($of_day eq "24") {
    $pp_24 = " selected";
    }
    if ($of_day eq "25") {
    $pp_25 = " selected";
    }
    if ($of_day eq "26") {
    $pp_26 = " selected";
    }
    if ($of_day eq "27") {
    $pp_27 = " selected";
    }
    if ($of_day eq "28") {
    $pp_28 = " selected";
    }
    if ($of_day eq "29") {
    $pp_29 = " selected";
    }
    if ($of_day eq "30") {
    $pp_30 = " selected";
    }
    if ($of_day eq "31") {
    $pp_31 = " selected";
    }
    
    if ($on_month eq "01") {
    $a_on = " selected";
    }
    if ($on_month eq "02") {
    $b_on = " selected";
    }
    if ($on_month eq "03") {
    $c_on = " selected";
    }
    if ($on_month eq "04") {
    $d_on = " selected";
    }
    if ($on_month eq "05") {
    $e_on = " selected";
    }
    if ($on_month eq "06") {
    $f_on = " selected";
    }
    if ($on_month eq "07") {
    $g_on = " selected";
    } 
    if ($on_month eq "08") {
    $h_on = " selected";
    }
    if ($on_month eq "09") {
    $i_on = " selected";
    }
    if ($on_month eq "10") {
    $j_on = " selected";
    }
    if ($on_month eq "11") {
    $k_on = " selected";
    }
    if ($on_month eq "12") {
    $l_on = " selected";
    } 
    if ($of_month eq "01") {
    $a1_of = " selected";
    }
    if ($of_month eq "02") {
    $a2_of = " selected";
    }
    if ($of_month eq "03") {
    $a3_of = " selected";
    }
    if ($of_month eq "04") {
    $a4_of = " selected";
    }
    if ($of_month eq "05") {
    $a5_of = " selected";
    }
    if ($of_month eq "06") {
    $a6_of = " selected";
    }
    if ($of_month eq "07") {
    $a7_of = " selected";
    } 
    if ($of_month eq "08") {
    $a8_of = " selected";
    }
    if ($of_month eq "09") {
    $a9_of = " selected";
    }
    if ($of_month eq "10") {
    $a10_of = " selected";
    }
    if ($of_month eq "11") {
    $a11_of = " selected";
    }
    if ($of_month eq "12") {
    $a12_of = " selected";
    } 
    
    <OPTION$a1_of VALUE=\"01\">January
    <OPTION$a2_of VALUE=\"02\">February
    <OPTION$a3_of VALUE=\"03\">March
    <OPTION$a4_of VALUE=\"04\">April
    <OPTION$a5_of VALUE=\"05\">May
    <OPTION$a6_of VALUE=\"06\">June
    <OPTION$a7_of VALUE=\"07\">July
    <OPTION$a8_of VALUE=\"08\">August
    <OPTION$a9_of VALUE=\"09\">September
    <OPTION$a10_of VALUE=\"10\">October
    <OPTION$a11_of VALUE=\"11\">November
    <OPTION$a12_of VALUE=\"12\">December
    </select><select NAME="of_day">
    <OPTION VALUE=\"\">
    <OPTION$pp_1 VALUE=\"01\">01
    <OPTION$pp_2 VALUE=\"02\">02
    <OPTION$pp_3 VALUE=\"03\">03
    <OPTION$pp_4 VALUE=\"04\">04
    <OPTION$pp_5 VALUE=\"05\">05
    <OPTION$pp_6 VALUE=\"06\">06
    <OPTION$pp_7 VALUE=\"07\">07
    <OPTION$pp_8 VALUE=\"08\">08
    <OPTION$pp_9 VALUE=\"09\">09
    <OPTION$pp_10 VALUE=\"10\">10
    <OPTION$pp_11 VALUE=\"11\">11
    <OPTION$pp_12 VALUE=\"12\">12
    <OPTION$pp_13 VALUE=\"13\">13
    <OPTION$pp_14 VALUE=\"14\">14
    <OPTION$pp_15 VALUE=\"15\">15
    <OPTION$pp_16 VALUE=\"16\">16
    <OPTION$pp_17 VALUE=\"17\">17
    <OPTION$pp_18 VALUE=\"18\">18
    <OPTION$pp_19 VALUE=\"19\">19
    <OPTION$pp_20 VALUE=\"20\">20
    <OPTION$pp_21 VALUE=\"21\">21
    <OPTION$pp_22 VALUE=\"22\">22
    <OPTION$pp_23 VALUE=\"23\">23
    <OPTION$pp_24 VALUE=\"24\">24
    <OPTION$pp_25 VALUE=\"25\">25
    <OPTION$pp_26 VALUE=\"26\">26
    <OPTION$pp_27 VALUE=\"27\">27
    <OPTION$pp_28 VALUE=\"28\">28
    <OPTION$pp_29 VALUE=\"29\">29
    <OPTION$pp_30 VALUE=\"30\">30
    <OPTION$pp_31 VALUE=\"31\">31
    </select><select NAME="on_month">
    <OPTION VALUE=\"\">
    <OPTION$a_on VALUE=\"01\">January
    <OPTION$b_on VALUE=\"02\">February
    <OPTION$c_on VALUE=\"03\">March
    <OPTION$d_on VALUE=\"04\">April
    <OPTION$e_on VALUE=\"05\">May
    <OPTION$f_on VALUE=\"06\">June
    <OPTION$g_on VALUE=\"07\">July
    <OPTION$h_on VALUE=\"08\">August
    <OPTION$i_on VALUE=\"09\">September
    <OPTION$j_on VALUE=\"10\">October
    <OPTION$k_on VALUE=\"11\">November
    <OPTION$l_on VALUE=\"12\">December
    </select><select NAME="on_day">
    <OPTION VALUE=\"\">
    <OPTION$dd_1 VALUE=\"01\">01
    <OPTION$dd_2 VALUE=\"02\">02
    <OPTION$dd_3 VALUE=\"03\">03
    <OPTION$dd_4 VALUE=\"04\">04
    <OPTION$dd_5 VALUE=\"05\">05
    <OPTION$dd_6 VALUE=\"06\">06
    <OPTION$dd_7 VALUE=\"07\">07
    <OPTION$dd_8 VALUE=\"08\">08
    <OPTION$dd_9 VALUE=\"09\">09
    <OPTION$dd_10 VALUE=\"10\">10
    <OPTION$dd_11 VALUE=\"11\">11
    <OPTION$dd_12 VALUE=\"12\">12
    <OPTION$dd_13 VALUE=\"13\">13
    <OPTION$dd_14 VALUE=\"14\">14
    <OPTION$dd_15 VALUE=\"15\">15
    <OPTION$dd_16 VALUE=\"16\">16
    <OPTION$dd_17 VALUE=\"17\">17
    <OPTION$dd_18 VALUE=\"18\">18
    <OPTION$dd_19 VALUE=\"19\">19
    <OPTION$dd_20 VALUE=\"20\">20
    <OPTION$dd_21 VALUE=\"21\">21
    <OPTION$dd_22 VALUE=\"22\">22
    <OPTION$dd_23 VALUE=\"23\">23
    <OPTION$dd_24 VALUE=\"24\">24
    <OPTION$dd_25 VALUE=\"25\">25
    <OPTION$dd_26 VALUE=\"26\">26
    <OPTION$dd_27 VALUE=\"27\">27
    <OPTION$dd_28 VALUE=\"28\">28
    <OPTION$dd_29 VALUE=\"29\">29
    <OPTION$dd_30 VALUE=\"30\">30
    <OPTION$dd_31 VALUE=\"31\">31
    </select>

    I also have a country list that has over 200 countries.

    Is there anyway to do this without having all those (if or elsif's)

    I looked at over 100 scripts, and all of them do it this way, I read everything I could find and nothing show how to make this shorter!

    TIA

    John
  2. #2
  3. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    87
    Then those scripts are poorly written.

    I'm going to do this once for you, I'm sure you can figure out how to adapt it.

    Short enough for you? The advantage here is that you can use the same CGI.pm construct to create the form element for any length list, and this makes it extremely easily to dynamically generate forms from a database.

    If you weren't using different values from labels, this would be simpler and we could do this only with an array, however in this case we must use an array to establish the order of the select elements. Really, using 1 to 12 for months is probably better than using month names anyway.

    If this is confusing, I suggest you read the CGI.pm docs, they are very well written. I wouldn't suggest coding any CGI with using CGI.pm, unless you like your programs to be buggy and more work to create and maintain. . . .

    Code:
    use CGI;
    
    my $q=CGI->new();
    
    #Define the hash of months.
    my %months=(1=>'January',2=>'February',3=>'March',4=>'April',
         5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',
         10=>'October',11=>'November',12=>'December');
    #Define the order of the months, given that hashes are unordered
    my @months=(1..12);
    
    
    # This will print a select form element with the months list and the selected option.
    # The -default will set whatever the default is that you want, in this case
    
    print 
         $q->popup_menu(-name=>'month',-values=>\@months,
         -labels=>\%months,-default=>$month);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    20
    It's easier to build select lists using a list and a loop, rather than printing
    them all out.

    Code:
    ## print a select list of the months with the current month selected
    @mon = qw(January February March April May June July August September October November December);
    
    print qq!<select name="whatever">\n!;
    $i=0;
    foreach $m (@mon) {
      $i++;
      print qq!<option value="$i"!;
      print qq! selected! if $current_month == $i;
      print qq!>$m\n!;
    }
    print qq!</select>\n!;
    
    ## print a select list with the current day selected
    print qq!<select name="whatever">\n!;
    foreach $d (1..31) {
      print qq!<option value="$d"!;
      print qq! selected! if $current_day == $d;
      print qq!>$d\n!;
    }
    print qq!</select>\n!;
  6. #4
  7. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    87
    The hand-rolled loop for creating form elements is more difficult, and more likely to break (e.g. you have to explicity define every behavior for every type of form element you want and worry about creating proper HTML, while CGI.pm takes care of all of that).

    Using CGI.pm for creating forms is powerful, cross-platform, and a major time saver. I highly suggest not reinventing this wheel, and taking some time to learn CGI.pm.

    You can easily create radio button groups, checkboxes with only some checkboxes checked, and upload forms. You can also easily create "sticky" forms that remember values entered automatically, without having to code this explicitly. This greatly simplifies the "submit, verify data , save to file or DB" cycle typical of complicated forms.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Over The Rainbow
    Posts
    0
    Rep Power
    0
    Hello


    I understand what you are saying, but the problem is the database spits out dates like this...........


    $on_month = 01,02,03,04,05,06,07,08,09,10,11,12

    not like

    1,2,3,4,5,6,7,8,9,10,11,12 0r jan,feb,march.....


    All I want to do is tag a select if $on_month = 02 then <option selected value=02>February</option>


    So doing this


    @mon = qw(01 02 03 04 05 06 07 08 09 10 11 12);

    does not work because (08, 09) are octal values!


    Then I can't do this because, I am not after what the month equals, but converting the what the month equals into (this select box selection is ("selected")


    As for using CGI.pm, I can't because the select boxes are wrapped with (css tags) based on the browser.


    Like this

    $b1 = <font class=ns4>;
    $b2 = class=ie;
    $b3 = </font>;

    $b1<select$b2 name=on_month>
    <option value=>some value</option></select>$b3


    Well I'll go do some more reading


    Thanks for your help


    John
  10. #6
  11. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    87
    I don't think you do understand what I'm saying. My previous post shows you how to create a select form element with a default selection. Read my post again!

    You can include class information in form elements created by CGI.pm. Read the docs for more information.

    You need to be clearer in explaining why this wouldn't work. So far I don't see anything that would cause you to be unable to use the shortcuts provided by CGI.pm.

    The only thing that would have to change here is:

    Code:
    my %months=('01'=>'January','02'=>'February','03'=>'March','04'=>'April',
         '05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September',
         '10'=>'October','11'=>'November','12'=>'December');
    
    my @months=qw(01 02 03 04 05 06 07 08 09 10 11 12);
    and you'd still have your select list with your "01" to "12" values.

    My previous post shows you how to create a select element with a conditionally selected default. Any hand-rolled way that you do this is going to be more work and less functional down the road.

    Read the CGI.pm docs. Not learning CGI.pm, and perl modules generally, is a cardinal sin if you want to be an effect CGI programmer.

    If you run the script I wrote above with the $month set to three, this is the output you get:

    Code:
    <select name="month">
    <option  value="1">January</option>
    <option  value="2">February</option>
    <option selected value="3">March</option>
    <option  value="4">April</option>
    <option  value="5">May</option>
    <option  value="6">June</option>
    <option  value="7">July</option>
    <option  value="8">August</option>
    <option  value="9">September</option>
    <option  value="10">October</option>
    <option  value="11">November</option>
    <option  value="12">December</option>
    </select>
    which looks to me like exactly what you want, in a much simpler and maintainable fashion.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    20
    John,

    @mon = qw(01 02 03 04 05 06 07 08 09 10 11 12);

    does not work because (08, 09) are octal values!


    Not sure what you mean here, '8' and '9' are not valid octal digits.

    Databases usually return all data in string format (even numbers), it is up to the calling program to convert them. And anyway, Perl will never convert a string literal with leading zeroes to a number, you must use the int(), hex() or oct() functions to do that. Run this test:

    Code:
    # string literal
    $num = "017";
    print "\$num = $num\n";
    print "\$num = ",int($num),"\n";
    print "\$num = ",oct($num),"\n";
    # numeric literal
    $num = 017;
    print "\$num = $num\n";
    Your results should be:
    $num = 017
    $num = 17
    $num = 15
    $num = 15

    So when you get your number back from the database, just use int($num) to convert it, it will strip off any leading zeroes and return the correct value.

    Don
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    20
    Hero Zzyzzx is correct in that you should use CGI.pm. Anytime you find yourself displaying html not output by CGI.pm you'll run into a place where you might a) not be following w3c standards, or 2) creating a place for problems later on.

    As I commented long ago in one of my early CGI apps:
    Code:
    # I used to not use CGI.pm, but then I realized what an idiot I was.
    Jon Coulter
    ledjon@ledjon.com
  16. #9
  17. No Profile Picture
    Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    Boston Ma.
    Posts
    1,529
    Rep Power
    0
    Hi

    It seems you don't want to change everything so just do this....

    Because 08,09 will cause problems you have to strip the first (0), but you can keep everything else the way you have it, even though it is not the best way to do it..........


    ## first convert the value from the database........

    $of_mf = $of_month;
    $on_mf = $on_month;
    $of_df = $of_day;
    $on_df = $on_day;

    ## now strip the first (0).............

    $on_mf =~ s/^0//g;
    $of_mf =~ s/^0//g;
    $on_df =~ s/^0//g;
    $of_df =~ s/^0//g;

    ## now tag your selected value.....

    my %mon($on_mf=>' selected');
    my %mfon($of_mf=>' selected');
    my %don($on_df=>' selected');
    my %dfon($of_df=>' selected');




    ## now just add these to your select boxes in your form, in numbered order....


    Example (on_month)

    <select name on_month>
    <option value=\"\">Month
    <option$mon{1} value=\"01\">01
    <option$mon{2} value=\"02\">02
    <option$mon{3} value=\"03\">03
    <option$mon{4} value=\"04\">04
    <option$mon{5} value=\"05\">05
    <option$mon{6} value=\"06\">06
    <option$mon{7} value=\"07\">07
    <option$mon{8} value=\"08\">08
    <option$mon{9} value=\"09\">09
    <option$mon{10} value=\"10\">10
    <option$mon{11} value=\"11\">11
    <option$mon{12} value=\"12\">12
    </select>


    ## Do the same for the rest...........



    Just a quick note, CGI.pm is a great tool if used right, but calling it just to split a single param or doing something simple, is really dumb, I see it used way to much, for just phasing a little form, and as for it not causing problems (bottle-neckies), it depends on the system you run it on...

    There is a very big difference, from using it in a linux (env) compared to a windoze (env)... and using PERLEX, ISAPI, or EXE/perl...



    F!
  18. #10
  19. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    87
    How exactly is it dumb to use CGI.pm for CGI programming? I'd really hate to have to come back to your script in 2 years and figure out how to change your drop-down lists. Talk about a lot of editing, versus just adding a new element into a hash, like you would with CGI.pm.

    I suppose if speed of development, stability, flexibility, maintainability, and true cross-platform development are dumb then yes, CGI.pm is a waste of time.

    We're talking literally milliseconds to use CGI.pm at compile time. For a vast majority of scripts, this is a non-issue. When it becomes one, your script is going to be under such heavy use that you'd happily convert it to work under mod_perl. This millisecond penalty gives you a way to parse form data and create dynamic forms that will always work, and work the way you expect it on pretty much any OS.

    Fataqui, you obviously don't understand anything that has been said, or the fact that when you use CGI.pm it jumps through hoops to delay loading unneeded sections of itself through a customized Autoloader.

    Telling a newbie to figure out form parsing and dynamic form creation themselves is terrible advice, and will lead to terrible, unmaintainable code.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    20
    Plus they don't need to manually strip the 0 anyway, just to $var = int($var); and it will convert it to a number (and obviously get rid of leading zeros). You can re-create the 2-char string with, among about a million other ways, a simple sprintf() call.
    Jon Coulter
    ledjon@ledjon.com

IMN logo majestic logo threadwatch logo seochat tools logo