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.


[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;
my $bools = {val1 => '', val2 => '', val3=> '', ... };
foreach $bool (keys $bools) {
  $bools->{$bool} = "checked" if($ref->{$bool} eq '1');
}
...
print <<EOF;
<input type=hidden name=val1 $bools->{val1}>
<input type=hidden name=val2 $bools->{val2}>
EOF
[/code]

Does this look like it might work? I think my syntax may be a bit off.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to