Gossamer Forum
Home : Products : DBMan : Customization :

Re: Fancy select field extract from db

Quote Reply
Re: Fancy select field extract from db In reply to
Maybe I have it. Smile

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






Subject Author Views Date
Thread Fancy select field extract from db handmade 4490 Apr 14, 2000, 6:50 PM
Post Re: Fancy select field extract from db
JPDeni 4362 Apr 14, 2000, 6:56 PM
Post Re: Fancy select field extract from db
JPDeni 4360 Apr 14, 2000, 7:34 PM
Post Re: Fancy select field extract from db
handmade 4387 Apr 15, 2000, 7:34 AM
Post Re: Fancy select field extract from db
JPDeni 4357 Apr 15, 2000, 9:39 AM
Post Re: Fancy select field extract from db
handmade 4394 Apr 15, 2000, 12:24 PM
Post Re: Fancy select field extract from db
JPDeni 4428 Apr 15, 2000, 2:11 PM
Post Re: Fancy select field extract from db
handmade 4369 Apr 15, 2000, 6:56 PM
Post Re: Fancy select field extract from db
JPDeni 4378 Apr 15, 2000, 8:57 PM
Post Re: Fancy select field extract from db
handmade 4363 Apr 16, 2000, 7:05 AM
Post Re: Fancy select field extract from db
JPDeni 4364 Apr 16, 2000, 12:22 PM
Post Re: Fancy select field extract from db
handmade 4376 Apr 16, 2000, 11:04 PM
Thread Re: Fancy select field extract from db
JPDeni 4368 Apr 16, 2000, 11:25 PM
Post Re: [JPDeni] Fancy select field extract from db
blacksol 4333 May 8, 2002, 10:54 PM