> Wiggins d'Anconia wrote: > > Andrew Gaffney wrote: > > > >> Wiggins d'Anconia wrote: > >> > >>> Andrew Gaffney wrote: > >>> > >>>> I have a Perl CGI script that runs a query against a MySQL table. I > >>>> have about 20 boolean values that I want shown as checkboxes in the > >>>> produced HTML, but I don't want to have to do something like the > >>>> below for every value. > >>>> > >>> > >>> The key to this is contriving good names for the check boxes, usually > >>> such that they can be looped over... > >> > >> > >> > >> That's not an option. The values have been defined months and months > >> ago and are hard-coded in a number of places. > >> > > > > Ah the power of poor planning.... > > It was planned pretty well at first...about 2 years ago, but things change. The table has > over doubled in number of fields since then. >
Right but at some point, aka around the fourth checkbox being added, someone should have realized that there would be more and added a loop just as you are doing now at around 20. In which case it was option 2 aka that refactoring wasn't being done as the system evolved, which just as they predict that means something major will have to be done to correct the situation at a later time, and that time has come.... > >>>> [code] > >>>> if($ref->{somevalue}) { > >>>> print "<input type=checkbox name=somevalue value=1 checked>"; > >>>> } else { > >>>> print "<input type=checkbox name=somevalue value=1>"; > >>>> } > >>>> [/code] > >>>> > >>> > >>> foreach my $index (1 .. 20) { > >>> print "<input type=checkbox name=somevalue$index " . > >>> ($ref->{"somevalue$index"} ? ' checked=checked' : '') . ">"; > >>> } > >>> > >>> The (exp ? string : string) is called the ternary operator, perldoc > >>> perlop for more. > >> > >> Will this work in a here doc? > > > > Don't know... god I hate here docs... give it a shot but I doubt it. > > Apparently, that's a big fat NO. Although, there is something else that would probably work: > > [code] > my $ref = $sth->execute; 'execute' (assuming we are talking DBI) returns a bool for success or failure, not a hashref. (Not sure if this was meant to be pseudo-code based on your statement below.) You will likely need a $ref = $sth->fetchrow_hashref; > my $bools = {val1 => '', val2 => '', val3=> '', ... }; > foreach $bool (keys $bools) { > $bools->{$bool} = "checked" if($ref->{$bool} eq '1'); > } If these are truly 'bools' then it is cleaner to check true/false rather than string equals to 1, true may not always be represented by 1 and undef will produce a warning rather than just being false. > ... > print <<EOF; > <input type=hidden name=val1 $bools->{val1}> > <input type=hidden name=val2 $bools->{val2}> > EOF > [/code] I suspect this isn't really all that is contained in your heredoc, otherwise why not put the input generation into the loop above? > > Does this look like it might work? I think my syntax may be a bit off. > Your syntax is a bit off (don't know if you wanted response on that), but your idea should work... http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>