Gossamer Forum
Home : Products : DBMan : Installation :

Select fields compiled from second database

Quote Reply
Select fields compiled from second database
I see you can have the select fields compiled based on the contents of a field in your present database, how do you have it take the fields from a second database?
Quote Reply
Re: Select fields compiled from second database In reply to
Is the structure of the second database the same as the structure of the original one?


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





Quote Reply
Re: Select fields compiled from second database In reply to
The second database is different structure.

The first database is to track repair requests. I want to use the second database to hold a list of my users with name, address etc. When I enter a repair request, I want to use a select field to select the users name. The select field will be made up of the contents of the second database.
Quote Reply
Re: Select fields compiled from second database In reply to
It can be done either way, but the code is a little different. (Actually, after I wrote it out, it doesn't seem to be much different at all.)

You will need to have the field names the same, though. If the field is "Name" in one, it will have to be called "Name" in the other.

This will only work with one field. If you should need to pull values from two fields in the other database, I'll have to write a whole new set of codes.

Define some variables in your .cfg file:

Code:
# Full Path and File name of the other database file.
$db_other_file_name = $db_script_path . "/other.db";

# Field number of the "Name" field in the other database
$other_fieldnum = 5;

Change other and 5 to match your database.

Add the following to db.cgi

Code:
sub build_select_field_from_other_db {
# --------------------------------------------------------
# Builds a SELECT field from the database.

my ($column, $value) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut);
my ($fieldnum, $found, $i) = 0;
open (DB, "<$db_other_file_name") or &cgierr("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 (!(grep $_ eq $fields[$other_fieldnum], @selectfields)) {
push (@selectfields, $fields[$other_fieldnum]);
}
}
close DB;

$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @selectfields) {
($field eq $value) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";

return $output;
}

In html_record_form, use the following to get your select field:

Code:
<tr><td>Name:</td>
<td>&nbsp;|;
print &build_select_field_from_other_db("Name",$rec{'Name'});
print qq|</td></tr>

(The above assumes that the code is within a "print qq|" statement.)

Change every instance of Name to match the fieldname in your database.

That should do it.

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





Quote Reply
Re: Select fields compiled from second database In reply to
Great mod, Carol.

I was wondering if you would be able and willing to modify the codes to allow multiple select fields from a different database to be generated. I have two fields in particular with many values (Academic Area and Departments) that would be labor intensive to update. If I could use this mod with a little spice, it would save me time in update the html.pl files.

Will this mod work with the fancy_select_field mod? I have the two fields built by the fancy mod.


If you could come up with code snippets that would guide me, I would appreciate it.

Thanks.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 04, 1999).]
Quote Reply
Re: Select fields compiled from second database In reply to
I would have to think long and hard about how to do a "build_fancy_select_field_from_db" subroutine. For one thing, the whole point of the fancy select field is that the value is different from the visible text. Where would you get the value?

To make multiple "build_select_field_from_other_db" fields, instead of

Code:
# Field number of the "Name" field in the other database
$other_fieldnum = 5;

you would need to have a list of all the fields in the other database:

Code:
# List of fields in other database
@other_db_cols = qw|ID Name Date Address|;

Then, in the code, after

Code:
my ($fieldnum, $found, $i) = 0;

add

Code:
for ($i = 0; $i <= $#other_db_cols; $i++) {
if ($column eq $other_db_cols[$i]) {
$other_fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}



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





Quote Reply
Re: Select fields compiled from second database In reply to
Thanks, Carol.

You wrote:

Quote:
Where would you get the value?

Well, both databases use the SAME fancy select fields. I was just hoping that there would be a way to pull the select field from one file to another. The "fields" are exactly the same. I know that it may be complicated. But if you or anyone comes up with a mod that for fancy_select_fields that works with this particular mod, I would greatly appreciate it.

Take a look at the sub build_fancy_select_field I provided for kiml in the following thread:

http://www.gossamer-threads.com/...um5/HTML/001363.html

I have the same codes in two different html.pl. Just looking for a way to conserve labor time in modifying the fancy_select_fields sub-routine. (As one of our Data Managers says:

Quote:
Better to spend more time up front getting something to work, then to repeat steps later on.

Thanks.

Smile

Regards,




------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 05, 1999).]
Quote Reply
Re: Select fields compiled from second database In reply to
If you have any code that is common between two or more databases, you can always create a separate file for that code and then include a "require" statement either in each of the .cfg files or in db.cgi. I'm almost ready to show a new application on my site which uses two databases as relational databases. There are a number of places where the code is in common, so I created a file called "format.pl." (I had to invent some name for it! Smile ) All of the code that is in common -- beginnings and endings of pages, the font definitions, some other variable definitions, the footer, &html_print_headers, some custom subroutines and a bunch of other stuff are all in "format.pl." Then I added a line in each of the .cfg files (it might work in the db.cgi file, too, but this works), which is identical to the "require ... \html.pl" line, except that it's "require ... \format.pl."


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





Quote Reply
Re: Select fields compiled from second database In reply to
Hi,

i want to use your build_select_field_from_other_db-fields but I got only the information of the dbkey field of the other database. Have I to change the .pl-file ,too? I wantto insert the ID-Number of the "one" database and the display of two other fields of this database(names) to insert the ID in a second(the add-) database!