Gossamer Forum
Home : Products : DBMan : Customization :

Build a Select Field from a Multi Select Field from Database

Quote Reply
Build a Select Field from a Multi Select Field from Database
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;
}
Quote Reply
Re: [digory] Build a Select Field from a Multi Select Field from Database In reply to
Try this as an example:

sub build_mult_select_field { ### this version works with spaces in select lists ********
# --------------------------------------------------------
# Builds a SELECT field based on information found in the database definition. Parameters are the column to build, a default value, a default name, and the size of the box.

my ($column, $value, $size) = @_;
my (@fields, $field, @selectfields, @lines, $line, $output);

$size || ($size=3);

@values = split (/\Q$db_delim\E/,$value);
@fields = split (/\,/, $db_select_fields{$column});

if ($#fields == -1) {
$output = "error building select field: no select fields specified in config for field '$column'!";
}

else {
$output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size><OPTION>---|;
foreach $field (@fields) {
$selected = 0;
foreach $value (@values) {
if ($value eq $field) {
$output .= "<OPTION SELECTED>$field\n";
$selected = 1;
}
}
unless ($selected) {
$output .= "<OPTION>$field\n";
}
}
$output .= "</SELECT>";
}
return $output;
}

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/