in reply to Help syntax?

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/&/&amp;/g; $s =~ s/</&lt;/g; $s =~ s/"/&quot;/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; }

Replies are listed 'Best First'.
Re^2: Help syntax?
by randallmarquis (Initiate) on Jul 12, 2010 at 01:49 UTC
    I tried your solution and it returns error codes... so it no work.

    This original DB was written back in the 90's, and could be updated I'm sure, but I need it to work for this site "lender-reviews.com" until we get funded.

    So it's critical to me. Here's a sample output of the data:
    http://lender-reviews.com/cgi/dbmod.cgi/lender/default.htm?view_records=1&ND=ND&option11=W&format=short&sb=26

    The 'fix' we are attempting to work on here is to assist in the editing of the data by admin and clients.

    I'm in Orange County, Southern California, and will take phone calls regarding this gladly: 949-722-7005

      I tried your solution and it returns error codes...

      Fix them, or let us know what they are. It's not like we can run the program.

      Update: $row_value was misspelled as $f_value in one place. Fixed in original. Is that all there was?

        error code was simple "error at line xxx" so no value... the f_value was multiple locations... which one is the error?