Gossamer Forum
Home : Products : DBMan : Customization :

Building select fields with related data

(Page 1 of 2)
> >
Quote Reply
Building select fields with related data
I would like to be able to build a select field which retrieves data from the list of related data and then pre fills some fields on the add form. Do I need the "fancy_mod"(?) for this? Pointers please.
Thanks again, Smile
Rob
Quote Reply
Re: Building select fields with related data In reply to
Any clues?
Please...... Smile
Quote Reply
Re: Building select fields with related data In reply to
I need more description of what you want to accomplish.


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
I want to accomplish the following:-

1) Select from a drop down related category for that user.

2) Dependent on 1) retrieve the data from the related table and auto-fill certain fields on add form.

This only needs to work on adding records. All tables use UserID and have a count as dbkey with the exception of users.

The perceived problem is that I'm using two "many" tables.

Thanks,
Rob

[This message has been edited by rcrossland (edited April 28, 2000).]
Quote Reply
Re: Building select fields with related data In reply to
Maybe I need to go to bed. I just can't picture what you're trying to do. I'll be back later.


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
Sleep???? Oh ok....you've earnt it!! Smile

In say an ordering system terms, it would be selecting a customer record before adding the order which by selecting the customer places some defaults in the fields.
Quote Reply
Re: Building select fields with related data In reply to
Then you would be pulling data from a "one" database (the users' information) and placing it in a "many" database (the orders). To get the info from the users database, use

&switch_to_users;
%rec2 = &get_record($db_userid);
&switch_to_whatever it was before

in sub html_record_form, after

my (%rec) = @_;

Then use $rec2{'FileName'} for the information from the users database.


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
Nearly....
There are three tables involved; users (one); contracts(many) and jobs(many). Ideally jobs is pulling data from contracts, a job can have more than one contract so hence I need to select which contract as the fist point in my job input and then based on that selection ideally fill in some fields on the add form. Users is not really needed other than it being the one side.

Currently contracts are related to users and jobs is standalone.

Thanks!
Rob Smile
Quote Reply
Re: Building select fields with related data In reply to
If a job can have more than one contract, are you looking to create a multiple select field?

Do you want all of the contracts listed in the select field or is there a way to tell which ones would be included?


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
If a job can have more than one contract, are you looking to create a multiple select field?

Yes please

Do you want all of the contracts listed in the select field or is there a way to tell which ones would be included?

The contracts are related to the user by the UserID so it's only the contracts for that user.

Thanks again (and again and again) Smile

Quote Reply
Re: Building select fields with related data In reply to
So you want a multiple select field from another db file, with the entries based on another field in the second file.

Lemme work on it a bit.


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
I think I got it.

The way this is set up, you'll only be able to use it for one select field, since several of the options are hard-coded into the subroutine. If you might want to use it for more than one form, I can give you that, too, but it can get pretty complicated to send all the variables to the sutroutine.

Code:
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 . "/default.db";

# The number of the field in the other .db file that holds the
# values you want in your select field
$fieldnum1 = 1;

# The number of the field in the other .db file that holds the userid.
$fieldnum2 = 2;

# 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><OPTION>---|;
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;
}

To use it, add

Code:
|;
print &build_limited_multiple_select_field_from_other_db("FieldName",$rec{'FieldName'})|;
print qq|

If you want to make a different size field (in height), either change the 3 in the line

Code:
$size or ($size = 3);

or add it to the call to the subroutine. If you want to do the latter and have, for example, 5 options show in the list, you would use

Code:
|;
print &build_limited_multiple_select_field_from_other_db("FieldName",$rec{'FieldName'},5)|;
print qq|



------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
Thanks JPD.

I'm having problems with the following line as it returns syntax errors (| related). I noticed on the multi_fancy_mod that the print & build line was formatted differently so I tried that but that does not appear on the add form either (code in sub html_record_form {)

|;print &build_limited_multiple_select_field_from_other_db("FieldName",$rec{'FieldName'})|;print qq|

I'm guessing that the |; is from a previous line maybe? Anyway it's the print&build that the cgi wrapper objects too.

(Yes fieldname has been replaced) Smile

Quote Reply
Re: Building select fields with related data In reply to
Yes. The

|;

is to close of a previous statement that began with

print qq|

Let me see your html.pl file. That's the only way I'll know what's going on.


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
/scratch head mode
I posted it, I'm sure I did....brb with it again then!

Code:
print qq|
<TABLE WIDTH="450" CELLPADDING=0 CELLSPACING=0 BORDER=1 BGCOLOR="#FFFFCC">

|;

if ($per_admin) {
print qq|
<TR><TD ALIGN="Right" VALIGN="TOP" WIDTH="175"><$font_color>UserID:</FONT></TD>
<TD VALIGN="TOP" WIDTH="475"> <INPUT TYPE="TEXT" NAME="UserID" SIZE="15" VALUE="$rec{'UserID'}" MAXLENGTH="15"></TD></TR>
|;
}
else {
print qq|
<input type="hidden" NAME="UserID" VALUE="$rec{'UserID'}">
|;
}
print qq|
print &build_limited_multiple_select_field_from_other_db("Name of the Client of this contract",$rec{'Name of the Client of this contract'},5);
<INPUT TYPE="hidden" NAME="TimeID" SIZE="11" VALUE="$rec{'TimeID'}">


[This message has been edited by rcrossland (edited May 08, 2000).]
Quote Reply
Re: Building select fields with related data In reply to
Your problem is in the last three lines. The should be:

Code:
print &build_limited_multiple_select_field_from_other_db("Name of the Client of this contract",$rec{'Name of the Client of this contract'},5);
print qq|<INPUT TYPE="hidden" NAME="TimeID" SIZE="11" VALUE="$rec{'TimeID'}">

I'm assuming there is more to your form than this and there is more printed after the hidden field. If not, be sure to end the code above with

|;



------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
Thanks, I now get the " error building select field: no fields specified! " message. I assume that the line...# The number of the field in the other .db file that holds the userid.
$fieldnum2 = 1; is correct in that although the cfg defines UserID as 0 (I've tried both values to no avail) 1 means the first field in the cfg.
Thanks again...
Rob
Quote Reply
Re: Building select fields with related data In reply to
In the current .cfg file (the one for the database you're adding to), what is the name of the field?


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
The field, $rec{'Name of the Client of this contract'} is from the "other" database as defined in the new sub routine, # The name of the other .db file
$db_other_file_name = $db_script_path . "/contract.db";

UserID is defined in all databases at position 0 (in cfg speak) and the extract from the other db is thus:
Code:
%db_def = (
'UserID' => [ 0, 'alpha', 15, 15, 1, '', ''],
'ContractID' => [ 1, 'alpha', -1, 15, 1, '', ''],
'Name of the Client of this contract'=> [ 2, 'alpha', 40, 40, 0, '', ''],
and I've used the following in your new sub:-
Code:
# The number of the field in the other .db file that holds the
# values you want in your select field - 3 = client name field
$fieldnum1 = 3;
# The number of the field in the other .db file that holds the userid.
$fieldnum2 = 0;

Thanks for having saintly like patience.. Smile
Rob
Quote Reply
Re: Building select fields with related data In reply to
I need to know what the name of the field is that you're writing *to* not the one that you're taking *from*. That's the name that you use in your "print &build_..." statement.

At the point that you build the select field in your "jobs" database, the script knows nothing about the structure of the "contracts" database.

------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
Ok, the select & build field from contracts (Name of this contract etc) needs to write into the jobs field .. 'Basic Time Claimed'

Thanks JPD
Quote Reply
Re: Building select fields with related data In reply to
So you want to build the subroutine with the name of the field in the jobs database:

Code:
print &build_limited_multiple_select_field_from_other_db("Basic Time Claimed",$rec{'Basic Time Claimed'},5);



------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
Nearly there! Smile
I now have a select field which lists the contracts for that user. I can select a value from there and the value is written to the "field" as you suggest. I think it's got lost in the thread (and my garbled communications) but ideally selecting the contract PULLS a value from the contract file e.g. basic working week and this is then written into the Basic hours field on the jobs file.
Does that make sense?....nearly there Smile
Quote Reply
Re: Building select fields with related data In reply to
Well, at least you're getting the select field. Smile

To pull the data from the other database, you'll need to add some stuff to sub add_record in db.cgi.

This is a multiple select field, right? So there can be multiple selections. (Duh! Smile )

Will multiple values from the "contracts" database be added together before they are added to the "jobs" database?


------------------
JPD






Quote Reply
Re: Building select fields with related data In reply to
No, the pulling of the basic hours from the contracts database is actually a hint, the user may overwrite with a new value. The process is thus:-

1. User selects one contract from selection list.
2. Contract selection pre-fills basic time field on jobs by pulling basic hours field from contract.
3. User adds data with either basic pre-filled value or overwrites with new value. Value stored in Basic Hours on jobs database.

Thanks, u da best Smile
> >