Gossamer Forum
Home : Products : DBMan : Customization :

Carol - one small question :)

Quote Reply
Carol - one small question :)
Your print&build from other db functions works great, thanks. However when there is only one record in the selection list (which is displayed but not highlighted) the value from that field is not added because it is not highlighted. Is there any way to make the $output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size>|; automatically highlight the value?

Hope I made sense?
Rob






Quote Reply
Re: Carol - one small question :) In reply to
There should be code to select the current value. Post the code you have and I'll see what's going on.


JPD
Quote Reply
Re: Carol - one small question :) In reply to
Here it is this (thanks):-
sub build_limited_multiple_select_field_from_other_db {
# --------------------------------------------------------
# Builds a SELECT field from an external database.
# Parameters are
# the column to which the value will be written -- $column
# a default value -- $value
# the height of the select field -- $size (optional)
my ($column, $value, $size) = @_;
my (@fields, $field, @selectfields, $selected, $fieldnum1, $fieldnum2,
, @lines, $line, $output, $found, @values);
$size or ($size = 3);
@values = split (/\Q$db_delim\E/,$value);
# Be sure to change the following to match your database
# The name of the other .db file
$db_other_file_name = $db_script_path . "/contract.db";
# The number of the field in the other .db file that holds the
# values you want in your select field
$fieldnum1 = 12;
# The number of the field in the other .db file that holds the userid.
$fieldnum2 = 0;
# End of things to edit.
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}
open (DB, "<$db_other_file_name") or &cgierr("Error in build_select_field_from_other_db.
Unable to open $db_other_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 ($fields[$fieldnum2] eq $db_userid) {
if (!(grep $_ eq $fields[$fieldnum1], @selectfields)) {
push (@selectfields, $fields[$fieldnum1]);
}
}
}
close DB;
$output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size>|;
foreach $field (sort @selectfields) {
$selected = 0;
foreach $value (@values) {
if ($value eq $field) {
$output .= "<OPTION SELECTED>$field";
$selected = 1;
}
}
unless ($selected) {
$output .= "<OPTION>$field";
}
}
$output .= "</SELECT>";
return $output;
}

Quote Reply
Re: Carol - one small question :) In reply to
The following code is designed to do exactly what you want:

Code:

foreach $value (@values) {
if ($value eq $field) {
$output .= "<OPTION SELECTED>$field";
$selected = 1;
}
}


I'm not sure what the problem could be. Are you sure there is already a value in the field?

JPD
Quote Reply
Re: Carol - one small question :) In reply to
The box displays the record from the other db ok, so that's fine. The problem is that unless the record is highlighted manually then the value is not updated into the field.

Quote Reply
Re: Carol - one small question :) In reply to
This is when you're adding a record? If so, I don't think there is a way to make it select the item.

Hmmmm. Maybe there is.

After

$output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size>|;

add

Code:

unless ($selectfields[1]) {
$output .= "<OPTION SELECTED>$selectfields[0]</SELECT>";
return $output;
}
I'm not certain it will work, but it might.

JPD
Quote Reply
Re: Carol - one small question :) In reply to
Cool That works fine thanks Smile
But....... only on single display option, a user with more than one related item doesn't get the first one highlighted...any further thoughts?? At worse I can tell the users to highlight I guess, thanks for all your help.

Quote Reply
Re: Carol - one small question :) In reply to
I thought you only wanted it highlighted if there was only one item in the list. That's what you seemed to be saying in your first post.

Take out what I gave you before. We'll work with the original code.

Change

Code:

$output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size>|;
foreach $field (sort @selectfields) {
$selected = 0;
foreach $value (@values) {
if ($value eq $field) {
$output .= "<OPTION SELECTED>$field";
$selected = 1;
}
}
unless ($selected) {
$output .= "<OPTION>$field";
}
}
$output .= "</SELECT>";
to

Code:

$output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size>|;
foreach $field (sort @selectfields) {
if (@values) {
$selected = 0;
foreach $value (@values) {
if ($value eq $field) {
$output .= "<OPTION SELECTED>$field";
$selected = 1;
}
}
unless ($selected) {
$output .= "<OPTION>$field";
}
}
else {
unless ($j) {
$output .= "<OPTION SELECTED>$field";
$j=1;
}
else {
$output .= "<OPTION>$field";
}
}

}
$output .= "</SELECT>";
JPD
Quote Reply
Re: Carol - one small question :) In reply to
Sorry about that, works fine and dandy. The lesson from this one is to perhaps describe more at the beginning (I was trying to save you hassle) and thus you end up spending less time digging for the requirement. Thanks again Smile.
Rob

Quote Reply
Re: Carol - one small question :) In reply to
Smile Well, I try to give people what they ask for. That's why I often spend several posts asking questions rather than giving code, especially if I'm not quite certain of what they want.

I'm glad we were able to figure out what you wanted and how to get it. Smile


JPD
Quote Reply
Re: Carol - one small question :) In reply to
U Da Best! Smile
Good job this forum is not in IRC, we would have to have a queue ticket system!! We Brits are good at queuing by the way... Smile