Hi all,
First off I can't thank all of you enough! The brave ones that posted questions as well as the the folks that took time to help out. I have taken bits and pieces from all of you and for that I am grateful. While I was building my application I found I needed a select field that came from data stored in a multi select field in the database. After lots of trial and error, below is what I came up with. I am not a perl programer so cut and paste into db.cgi at your own risk :)
digory
sub build_select_field_from_multi_from_db {
# -----------------------------------------------------------
# Builds a SELECT field from a field in the database that has multiple elements.
# Call as follows: print &build_select_field_from_multi_from_db("mf",$rec{'mf'});
# Where mf is the name of the multiple select field in your database.
# -----------------------------------------------------------
my ($column, $value, $name) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut, @fix, @fixed, @fixed_selectfields);
my ($fieldnum, $found, $i, $f) = 0;
$name || ($name = $column);
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode($line);
if (!(grep $_ eq $fields[$fieldnum], @selectfields)) {
push (@selectfields, $fields[$fieldnum]);
}
}
close DB;
#split out the individual elements stored in muiti select field.
foreach $selectfields(@selectfields){
@fix = split(/,/, $selectfields);
push (@fixed, @fix);
}
#get rid of spaces at front and back of each item.
my $replace = "";
foreach $fixed(@fixed) {
$fixed =~ s/^\s*|\s*$/$replace/g;
}
#strip list down to unique elements.
foreach $fixed(@fixed) {
if (!(grep $_ eq $fixed, @fixed_selectfields)) {
push (@fixed_selectfields, $fixed);
}
}
$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @fixed_selectfields) {
($field eq $value) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";
return $output;
}
First off I can't thank all of you enough! The brave ones that posted questions as well as the the folks that took time to help out. I have taken bits and pieces from all of you and for that I am grateful. While I was building my application I found I needed a select field that came from data stored in a multi select field in the database. After lots of trial and error, below is what I came up with. I am not a perl programer so cut and paste into db.cgi at your own risk :)
digory
sub build_select_field_from_multi_from_db {
# -----------------------------------------------------------
# Builds a SELECT field from a field in the database that has multiple elements.
# Call as follows: print &build_select_field_from_multi_from_db("mf",$rec{'mf'});
# Where mf is the name of the multiple select field in your database.
# -----------------------------------------------------------
my ($column, $value, $name) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut, @fix, @fixed, @fixed_selectfields);
my ($fieldnum, $found, $i, $f) = 0;
$name || ($name = $column);
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode($line);
if (!(grep $_ eq $fields[$fieldnum], @selectfields)) {
push (@selectfields, $fields[$fieldnum]);
}
}
close DB;
#split out the individual elements stored in muiti select field.
foreach $selectfields(@selectfields){
@fix = split(/,/, $selectfields);
push (@fixed, @fix);
}
#get rid of spaces at front and back of each item.
my $replace = "";
foreach $fixed(@fixed) {
$fixed =~ s/^\s*|\s*$/$replace/g;
}
#strip list down to unique elements.
foreach $fixed(@fixed) {
if (!(grep $_ eq $fixed, @fixed_selectfields)) {
push (@fixed_selectfields, $fixed);
}
}
$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @fixed_selectfields) {
($field eq $value) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";
return $output;
}