Gossamer Forum
Home : Products : DBMan : Customization :

Enter order staring first with "One" relational database

Quote Reply
Enter order staring first with "One" relational database
"One" database is called clients. "Many" database is called orders.

When I click "add", I'd like to first be able to choose a client, then click submit or continue, then enter the order. When I later list the order, you will be able to view the client info. I guess a single drop down to apply a client to an order would work, but I'm forcing the user to choose a client before being able to enter the order.

I searched a bunch of different scenarios and didn't seem to see any like this. Maybe I just didn't understand them completely.

Chris
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
So you want the user to select a client, send that information to the script and then enter the order?

It's doable, but would be a real pain to do. You really can't just have a select field with the clients' names on the same form as the orders?


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Enter order staring first with "One" relational database In reply to
I can . . . didn't know how difficult it was.

Anyway, after going through all the solutions to add a drop down, maybe too quickly, I couldn't figure any of them out. Which mod or FAQ should I be looking at to have a drop down on the html_record_form to attach the data of a "client" to an "order"?

Thanks!!!

Chris
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
What you're going to need to do is to create a select field from another database. There have been a lot of permutations of creating select fields from all sorts of things. There should already be one out there somewhere, but I'm not sure where it is.

I'll think about it for a bit and see what I can do.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Enter order staring first with "One" relational database In reply to
I have been using the FAQ, "Building select fields with related data". It's more for "multiple select" which I don't need, but unfortunately it has yet to work for me. I do all is says then I do the, "To use it, add code:" and the db no longer works. Don't know what I'm missing.

Is that the correct FAQ for what I am trying to do?

Chris
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
It's probably best to start from scratch.

My assumption is that your customer database has fields for customer ID, customer first name and customer last name. You would like to create a select field where you can select a customer's name, and which would result in the customer ID being inserted into the form on the orders database. Is this right?

I've got it worked out conceptually, but I can't remember how to do the type of sort I want to do so that the customers' last names are alphabetical. I'll do some looking around for it. It's just been a while since I've done much programming, so I forgot. :-)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Enter order staring first with "One" relational database In reply to
Actually I did start from scratch. I used your configurator to create my cfg's and *_html.pl's. I made sure the relational swith_to's were in there, although maybe I missed something..

My pl's are here if you would like to look at them. clients_html.txt orders_html.pl and the cfg's clients.txt orders.cfg.

I've been using dbman for many years for a bunch of different stuff and have created some really cool things with it. This is the very first time I've played with relational. If I could just figure out this thing, I could run with it. Thanks!!!

Chris
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
I need to know which field in "orders" is related to the clientID field in "clients." It is really helpful to have the related field in both databases to have the same name.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Enter order staring first with "One" relational database In reply to
Now I don't quite understand that. I have the key intorderno for the orders.db. Am I really supposed to have a relative key in the other db? There is no such info as intorderno (interior order no) in the clients.db nor is there info called clientID (the key for clients.db) in orders.db.

What do you suggest?

Chris
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
If you want to relate records in the orders.db file to records in the client.db file, there needs to be a field that the two .db files have in common -- the $db_key field from the "one" side of the relationship. You have to have a clientID field in the orders database. Otherwise there is no way for the script to know which client is connected to which records.

That's the purpose of the select field that we're talking about.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
Okay, I have started from SCRATCH and use the item and user db's as in the relational mod. I then used the FAQ, 948 Building select fields with related data. When I click ADD, I get the form with the error, "error building select field: no fields specified!"

I can usually fix this on a single db, but the relational thing is confusing me. I think it must have to do with the sub at the bottom of item_html.pl, but not sure.

My pl's and cfg's are here if you would like to look at them. item_html.txt user_html.txt and the cfg's item.txt user.txt.

By the way, the FAQ has an error at the end where it says, "To use it, add code:" Change:
Code:
|;
print &build_limited_multiple_select_field_from_other_db("FieldName",$rec{'FieldName'},5)
|;
print qq|


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

Chris

Last edited by:

kcgbjc: Feb 19, 2006, 8:58 PM
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
Don't use that subroutine. I'm going to build you one from scratch. (That's the "scratch" I was talking about.) I just needed to know how your database was set up and how you wanted the select field.

So... how about a select field that lists the users names by first and last name (alphabeticallyby last name), but enters the UserID into the items db?

I can't change anything in that FAQ. Good that you mentioned it, though, so LoisC can edit it.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
I think this should work. I don't have a database set up to test it, though, so I'll need you to test it for me. :-)

Code:

sub build_select_field_from_related_db {
# --------------------------------------------------------
# Builds a SELECT field from the database.
# This subroutiine hard-codes the data needed for to enter the key field
# for the "one" side of a relational database into a field in the "many"
# side. Since it is hard-coded, it might be best to place this in the
# html.pl file for the "many" side.
# This also assumes that the field the two databases have in common has the
# same name in both.
my $value = @_;
my (@fields, %selectfields, @lines, $line, $output);

&switch_to_user; # Change this line to reflect the name of the "one" side of your database
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);

# I used a hash here to make alphabetizing easier. In this case, field 2 is the last name of a person
# and field one is the first name. When we print them out, all we have to do is sort the hash keys and
# everything will be in alphabetical order.
$selectfields{$fields[2]$fields[1]}[0] = $fields[$db_key_pos]; # This is the key value from the "one" side
$selectfields{$fields[2]$fields[1]}[1] = "$fields[1] $fields[2]"; # This is what will be seen in the select field so
# you don't have to memorize the number
}
close DB;

$output = qq|<SELECT NAME="$db_cols[$db_key_pos]"><OPTION>---|;
foreach $key (sort keys %selectfields) {
($selectfields{$key}[0] eq $value) ?
($output .= qq|<OPTION value="$selectfields{$key}[0]" SELECTED>$selectfields{$key}[1]|) :
($output .= qq|<OPTION value="$selectfields{$key}[0]">$selectfields{$key}[1]|);
}
$output .= qq|</SELECT>|;
&switch_to_item;
return $output;
}


To use it, in your html.pl file for items, just use

print &build_select_field_from_related_db($rec{'UserID'});


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Enter order staring first with "One" relational database In reply to
First of all, THANKS for the time you spend. You must have a love or be getting something out of it considering the time you have spent over the years that I have been following the scripts.

I put your sub at the end of my item_html.pl with no edits. I put the print line in my html_record_form and all I get is an internal error. I tried tweaking a couple of things to no avail.

I have now set it back to where you told me to put it. Want to test it? I've attached the db.

Chris

Last edited by:

kcgbjc: Feb 20, 2006, 8:51 AM
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
That's what I get for being too clever. :-)

Change

Code:
$selectfields{$fields[2]$fields[1]}[0] = $fields[$db_key_pos]; # This is the key value from the "one" side
$selectfields{$fields[2]$fields[1]}[1] = "$fields[1] $fields[2]"; # This is what will be seen in the select field so


to

Code:

$hashkey = "$fields[2]$fields[1]";
$selectfields{$hashkey}[0] = $fields[$db_key_pos]; # This is the key value from the "one" side
$selectfields{$hashkey}[1] = "$fields[1] $fields[2]"; # This is what will be seen in the select field so


Sorry 'bout that.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Enter order staring first with "One" relational database In reply to
Haven't had time to fully test it but it is working as it should on first glance!!! The names come up from the other database just like they should!!!

I've attached a zip of the database that should be the completion of the relational mod with the drop down box for future reference.

Thanks JPDeni!! You are awesome. Above and beyond once again! I'll tell ya how it works once I personallize the databases.

Chris
Quote Reply
Re: [kcgbjc] Enter order staring first with "One" relational database In reply to
I'm glad I could help.

I do this for two reasons. It makes me feel good to help people and I love logic problems. Sometimes I want to pull my hair out, but it feels so good when it works.


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