Please wrap computer text (code, data, output) in <c>...</c> tags. It'll even handle escaping for you.
@selectfields needs to hold two values per record, the id and the value of the selected column.
sub text_to_html { my $s = shift; $s =~ s/&/&/g; $s =~ s/</</g; $s =~ s/"/"/g; return $s; } sub build_select_field_from_db { # -------------------------------------------------------- # Builds a SELECT field from the database. my ($column, $value, $name) = @_; $name ||= $column; my $fieldnum; for my $i (0..$#db_cols) { if ($column eq $db_cols[$i]) { $fieldnum = $i; last; } } if (!defined($fieldnum)) { return "error building select field: no fields specified!"; } open (my $db_fh, "<", $db_file_name) or &cgierr("unable to open $db_file_name. Reason: $!"); my @lines = <$db_fh>; close $db_fh; my @selectfields; foreach my $line (@lines) { next if $line =~ /^#/; # Skip comment lines. next if $line =~ /^\s*$/; # Skip Blank Lines. chomp ($line); my @fields = &split_decode ($line); my ($row_id, $row_value) = @fields[0, $fieldnum]; if (!(grep $_[1] eq $fields[$fieldnum], @selectfields)) { push @selectfields, [ $fields[0], $fields[$fieldnum] ]; } } my $output = qq|<SELECT NAME="$name">|; $output .= "<OPTION>---"; foreach (sort { $a->[1] cmp $b->[1] } @selectfields) { my ($row_id, $row_value) = @$_; my $esc_id = text_to_html($row_id); my $esc_value = text_to_html($row_value); my $selected = $value eq $row_value ? ' selected' : ''; $output .= qq{<option$selected value="$esc_id">$esc_value}; } $output .= "</SELECT>"; return $output; }
Update: That grep bothers me.
sub build_select_field_from_db { # -------------------------------------------------------- # Builds a SELECT field from the database. my ($column, $value, $name) = @_; $name ||= $column; my $fieldnum; for my $i (0..$#db_cols) { if ($column eq $db_cols[$i]) { $fieldnum = $i; last; } } if (!defined($fieldnum)) { return "error building select field: no fields specified!"; } open (my $db_fh, "<", $db_file_name) or &cgierr("unable to open $db_file_name. Reason: $!"); my @lines = <$db_fh>; close $db_fh; my %selectfields; foreach my $line (@lines) { next if $line =~ /^#/; # Skip comment lines. next if $line =~ /^\s*$/; # Skip Blank Lines. chomp ($line); my @fields = &split_decode ($line); my ($row_id, $row_value) = @fields[0, $fieldnum]; $selectfields{$row_value} = $row_id; } my $output = qq|<SELECT NAME="$name">|; $output .= "<OPTION>---"; foreach my $row_value (sort keys (%selectfields)) { my $row_id = $selectfields{$row_value}; my $esc_id = text_to_html($row_id); my $esc_value = text_to_html($row_value); my $selected = $value eq $row_value ? ' selected' : ''; $output .= qq{<option$selected value="$esc_id">$esc_value}; } $output .= "</SELECT>"; return $output; }
In reply to Re: Help syntax?
by ikegami
in thread Help syntax?
by randallmarquis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |