Maybe I have it.
Try adding the following to db.cgi:
# --------------------------------------------------------
# Builds a fancy SELECT field from the database.
my ($column, $select, $value) = @_;
my (@fields, $field, @selectfields, %values, @lines, $line, $ouptut);
my ($fieldnum1, $found1, $fieldnum2, $found2, $i) = 0;
$name or ($name = $column);
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum1 = $i; $found1 = 1;
}
if ($select eq $db_cols[$i]) {
$fieldnum2 = $i; $found2 = 1;
}
}
if (!$found1 or !$found2) {
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[$fieldnum1], @selectfields)) {
push (@selectfields, $fields[$fieldnum1]);
$values{$fields[$fieldnum1]} = $fields[$fieldnum2]
}
}
close DB;
$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @selectfields) {
if ($values{$field} eq $value) {
$output .= qq|<OPTION VALUE="$values{$field}" SELECTED>$field) |;
$output .= qq|<OPTION VALUE="$values{$field}">$field|;
}
}
$output .= "</SELECT>";
return $output;
}
To use it in sub html_record_form, enter
print &build_fancy_select_field_from_db("FieldnameA","FieldnameB","$rec{'FieldnameB'}"); print qq|
where FieldnameA is the name of the field where the values you want to show in your select field are and FieldnameB is the name of the field which holds the values you want to be added to your database.
I'm not sure about when you said
Might there be blank entries in the fields you're pulling from?
------------------
JPD
Try adding the following to db.cgi:
Code:
sub build_fancy_select_field_from_db { # --------------------------------------------------------
# Builds a fancy SELECT field from the database.
my ($column, $select, $value) = @_;
my (@fields, $field, @selectfields, %values, @lines, $line, $ouptut);
my ($fieldnum1, $found1, $fieldnum2, $found2, $i) = 0;
$name or ($name = $column);
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum1 = $i; $found1 = 1;
}
if ($select eq $db_cols[$i]) {
$fieldnum2 = $i; $found2 = 1;
}
}
if (!$found1 or !$found2) {
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[$fieldnum1], @selectfields)) {
push (@selectfields, $fields[$fieldnum1]);
$values{$fields[$fieldnum1]} = $fields[$fieldnum2]
}
}
close DB;
$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @selectfields) {
if ($values{$field} eq $value) {
$output .= qq|<OPTION VALUE="$values{$field}" SELECTED>$field) |;
$output .= qq|<OPTION VALUE="$values{$field}">$field|;
}
}
$output .= "</SELECT>";
return $output;
}
To use it in sub html_record_form, enter
Code:
|; print &build_fancy_select_field_from_db("FieldnameA","FieldnameB","$rec{'FieldnameB'}"); print qq|
where FieldnameA is the name of the field where the values you want to show in your select field are and FieldnameB is the name of the field which holds the values you want to be added to your database.
I'm not sure about when you said
Quote:
All except blank fields must be filled in.Might there be blank entries in the fields you're pulling from?
------------------
JPD