Gossamer Forum
Home : Products : DBMan : Customization :

build_select_field_from_db based on two fields

Quote Reply
build_select_field_from_db based on two fields
Hey.

IŽde like to have 3 Categories:

1. Category (i.e. Home and Garden) in default.cfg = Category1
2. Category (i.e. Living) in default.cfg = Category2
3. Category (i.e. whatever) in default.cfg = Category3

Now iŽde like to have a select field build out of all entry in Category1, 2 and 3.

Is that possible ?

Thanks for your help.

ups
Quote Reply
Re: [ups] build_select_field_from_db based on two fields In reply to
do you want three choices for one category? or multiple choices for three different categories?

if you just have one category field with the three choices you list, just set up one category field in cfg. then, list the choices in the build_select_field variable.
Quote Reply
Re: [delicia] build_select_field_from_db based on two fields In reply to
Thank you for your reply.

IŽve two fields and i want to have all items of the to fields used within on select_field_from_db.

Thank you
ups
Quote Reply
Re: [ups] build_select_field_from_db based on two fields In reply to
not sure i understand. sounds like you have two fields and both have the same choices? if so, set up two separate fields. then include both and the complete lists in the build_select variable.
Quote Reply
Re: [delicia] build_select_field_from_db based on two fields In reply to
Let me explain ...

I have a list of Companies.

Each Company can chose three diferent categories they are working in from a list of hundereds of possible categories.

To do this they joining category 1, category 2 and category 3.

By searching for companies by categories there should be a build_select_field_from_db to list only categories which have already entries ie. companies in it.

If i would have a select_field build only by existing entries from category 1 then you would not find anything which is included in category 2 or 3.

So the build_select_field_from_db should show all entries from category 1 - 3 together.

Hope this is clear ...

Thanks
ups
Quote Reply
Re: [ups] build_select_field_from_db based on two fields In reply to
ok, i understand now. but it's over my head perl-wise. maybe one of the perl gurus here can help.
Quote Reply
Re: [ups] build_select_field_from_db based on two fields In reply to
One suggestion would be to create another field in your database which will hold the contents of the 3 categories fields combined.

Check out the FAQ noted below under the Category "Fields" for the following thread references:


Copy contents of field1 into field2

Copy one field into another when adding records

Make a field a value of two other fields

merge three fields into one

There may be others but one of the above will help you to find a solution.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [ups] build_select_field_from_db based on two fields In reply to
I work best with an example, so I'm going to create one that seems to be what you are going for.

My database is about farmers. Each record is the information about one farmer. Each farmer can grow three (or fewer?) fruits and/or vegetables, but no more.

These are the records I currently have:
Name: Old MacDonald
Crop1: Apples
Crop2: Oranges
Crop3: Artichokes

Name: Farmer Inthedell
Crop1: Artichokes
Crop2: Green beans
Crop3: Kumquats

Name: Mr. MacGregor
Crop1: Lettuce
Crop2: Kumquats
Crop3: Apples

Name: Mr. Greenjeans
Crop1: Plums
Crop2: Grapes
Crop3: Bananas

In a search form, I want to have all of the fruits and vegetables from all three fields in a select field and I would want it to search across all three Crop fields.

(This would actually be more efficient and elegant if you used a relational database structure with your Categories in a separate small table. That way, you could have more than three categories for each company. But we'll work with what you have.)

Using my data, the select field would then include:

Apples
Artichokes
Bananas
Grapes
Green beans
Kumquats
Lettuce
Oranges
Plums

And if I did a search for Apples, I should get Old MacDonald and Mr. MacGregor, even though Apples is in a different field in each of the records.

This is one of those things you need to hard-code into the db.cgi file. Or maybe someone else could figure out how to make it less hard-coded.

Code:

sub build_select_field_from_multiple_fields {
# --------------------------------------------------------
# Builds a SELECT field from the database.
my ($value) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut);
my (@fieldnum, $num, $found, $i) = 0;
# can not be the name of any of the fields you are using
# Make it something descriptive, though.
$name = "Category";

# Define which fields you want to include -- the *numbers* of the fields, not the names
$fieldnum[0] = 4;
$fieldnum[1] = 5;
$fieldnum[2] = 6;
# Theoretically you can use however many you want.
# The more you use, especially with a large database, the more it will slow down your application.

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);
foreach $num (@fieldnum) {
if (!(grep $_ eq $fields[$fieldnum[$num]], @selectfields)) {
push (@selectfields, $fields[$fieldnum[$num]]);
}
}
}
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;
}


That will make your select field, but that's only part of the battle. Other things that must be done for this to work...
  • Use by including &build_select_field_from_multiple_fields($in{Category}) in your form (or whatever you are naming your select field)
  • Create a separate form for searching (you don't want this in your "add record" form)
  • Set up sub query so that if there is input from this select field, the input will be copied to all of the category fields and $in{'ma'} is set to "on."



JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.