Gossamer Forum
Home : Products : DBMan : Customization :

Print Only ID in Main Database and other Fields to seperate text files

Quote Reply
Print Only ID in Main Database and other Fields to seperate text files
It's been a long while since I last posted.

Anyway, I am asking for assistance if anyone can help on this one!

I have a cfg file with 51 fields. I want to do the following when a record is added:
  1. Only print the ID field in the default db (ID field is the db key track).
  2. For the remaining 50 fields, print out a separate text file database with a pipe delimited format. Each record (unique ID) would have its own associated text file database.


Example:

Sample of fields: ID|FirstName|LastName|etc|etc
  1. Fill out form with 51 fields and hit Add Record button.
  2. DBman processes form, opens default db, prints ID, closes default db.
  3. DBman then creates a new text file, names it FirstNameLastName.txt, prints the remaining 50 fields in pipe delimited format then closes that db.
  4. Next user comes along and repeats the process, ID is printed in default db and remaining fields are printed in its own unique database with its own unique FirstNameLastName.txt format


Basically what it comes down to is that I am using DBman to process the form data and create the files. Another program will use those files. I don't even really need it to work with the modify function (although it would be nice, if possible) or the delete, search, etc functions.

Anyone have a solution? If you need additonal details, please ask.

Thanks!
Quote Reply
Re: [BrianYork] Print Only ID in Main Database and other Fields to seperate text files In reply to
You'll want to stick the subroutine under "add_success" and/or "modify_success".

Here is a 'quick and dirty solution' (under html_add_success):

Code:
## Writes data to File
open(FILE, ">$in{'fname'}$in{'lname'}.txt") or die "Error Writing to file\n";
select(FILE);

print qq|$in{'fname'}|$in{'lname'}|$in{'middle'}|$in{'etc'}|;

close(FILE);

(Notice the script is using $in instead of $rec) this way you can pass info from fields that aren't part of the database. This *should* work, but I haven't tested it. You'll have to play around with where you put it under add success.
Quote Reply
Re: [Watts] Print Only ID in Main Database and other Fields to seperate text files In reply to
Watts:

Thanks for the reply. Tried it and it does not work.

Here is my code in sub html_add_success:

Code:
open (export, ">/databases/$in{$First_Name}$in{$Last_Name}.txt");print export qq~$rec{'ID'}|$rec{'Date_Added'}|$rec{'Added_By'}|$rec{'Added_By_Email'}|$rec{'Name'}~;close export;


The text file does not print in the databases folder at all. Of course, I set permissions of the databases folder to 777 for testing purposes. Also, I had to use print qq~ in lieu of print qq| because the pipes seemed to cause the script to return server errors.

Only works if I change the code as follows:

Code:
open (export, ">/databases/$in{$db_key}.txt");print export qq~$rec{'ID'}|$rec{'Date_Added'}|$rec{'Added_By'}|$rec{'Added_By_Email'}|$rec{'Name'}~;close export;



You notice that I had to change

open (export, ">/databases/$in{$First_Name}$in{$Last_Name}.txt");

to

open (export, ">/databases/$in{$db_key}.txt");

This will not work for me. I need to new text file databases to be named FirstNameLastName.txt.

Any other suggestions? This will solve my main problem but it still does not address writing only the Id to the default db and the remaining fields to the individual new databases.

Thanks for any additonal help!
Quote Reply
Re: [BrianYork] Print Only ID in Main Database and other Fields to seperate text files In reply to
Not sure if you were just in a hurry posting here, but double check your syntax... it should be

$in{'fieldname'}

Notice where the the dollar sign is and where the single quotes are.. You should be able to name the file anything you want.

Good Luck!
Quote Reply
Re: [Watts] Print Only ID in Main Database and other Fields to seperate text files In reply to
Watts,

Thanks! Removed the extra dollar signs. Goes to show that no matter how many times you read over something, you can still miss the obvious.

That resolves half of my problem. How can I get the default db to only record the ID and none of the other fields? I probably can have the record added to the default db normally, then add a routine at the end of add success that opens it back up and deletes all of the columns except the ID. I'm sure this is more taxing on the server processing time, but it should not be to bad. Anyway, I'm not sure how to do that either.

The reason I need to do this is that my default db will get large very quickly. The ID in only the default db with the rest of the data in a unique text file helps me resolve this.

Any addtional help to resolve the second half of the problem would be appreciated!

Last edited by:

BrianYork: Jul 8, 2003, 4:50 AM
Quote Reply
Re: [BrianYork] Print Only ID in Main Database and other Fields to seperate text files In reply to
Looking at db.cgi, it appears that this line of sub add_record does the actual writting to the DB file.

Code:
print DB &join_encode(%in);

Thus if I were to attempt this I would start with the join_encode sub (copy it to a new sub) and see if I could modify it to only write the desired fields.

Maybe Watts has a few ideas, I have mostly questions at this point. It seems that the database structure you have choosen will work for a specific application (we hope). I'm not sure about how you will handle multiple records for a user, or how you can search fileds that are in the text files but not in the database?
Quote Reply
Re: [BrianYork] Print Only ID in Main Database and other Fields to seperate text files In reply to
Are you using auto-generate? If not, simply have only the one field you want in the default.cfg file (ID). The other form fields can be added to the html file but won't get recorded in the db. You can print the field data by using "$in" instead of "$rec". You could also check out Joematt's suggestion as well and not write anything to the db.
Quote Reply
Re: [Watts] Print Only ID in Main Database and other Fields to seperate text files In reply to
Thanks Watts for pointing out the difference between the $in and the $rec, that makes good sense. I may need that inforamtion in the future.

I guess I was having a mental block as to how he could write a record for which there was no configuration information, but clears it up.

BrianYork, has you considered the relational MOD? If the poweres of the relational MOD are not required for your DB setup, just ignore that suggestion.
Quote Reply
Re: [joematt] Print Only ID in Main Database and other Fields to seperate text files In reply to
Thanks Watts and joematt!

That does what I need it to. I also did not know that you could pass info by using $in while not defining the field in the config file. This is useful sometimes.

Okay, phase 2 of my project.

I have two unique databases running with two unique html.pl files, db.pl files and pass files. When the user adds a record in the one database program (we'll call it the owner database), I want at the add success stage in that database to open the password file for the second database (we'll call it the users database) and add a user, permissions, email, etc. based on form fields from the owner database.

Here is code I have that works so far:

Code:
sub add_to_pass {
# --------------------------------------------------------
&html_print_headers; %rec = &get_record($in{$db_key}); open (PASS, ">$other_pw_file") or &cgierr ("unable to open: $other_pw_file.\nReason: $!"); if ($db_use_flock) {

flock(PASS, 2) or &cgierr("unable to get exclusive lock on $other_pw_file.\nReason: $!");


}

@lines = <PASS>;

my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
my $encrypted = crypt("user$in{'ID'}", $salt);


print PASS "$in{'Name'}:$encrypted:1:1:1:1::$in{'Email'}\n";

close PASS;

}


The above works. I call it in sub html_add_success by using &add_to_pass(%rec);. I define it in the config file for the owner database program using

# Full path and file name of the other password file.
$other_pw_file = $db_script_path . "/other.pass";

When I add the record in the owners database program, add the add success stage it opens the other.pass file and outputs the following:

user:XPTSbgjVOvNdY:1:1:1:1::joe.user@server.com

As you can see, it formats the other.pass with the user name, an encrypted password, the persmissions I am giving for the user database and the email address.

Here is the problem:

Every time I use it, instead of adding the next user on a new line, it overwrites the previous users (therefore, I can only ever have 1 user in the password file). What code do I need to add so that when the other.pass is opened to be modified that the program will amend to the password file and not overwrite all of my users?

Thanks and ask questions if you need to!

By the way, joematt, without getting into a lot of detail, this is a very customized DBman application and the way I am proceeding may not be exactly the best way to do it, but it will suit my needs. Once I get this little problem fixed, I am on my way!
Quote Reply
Re: [BrianYork] Print Only ID in Main Database and other Fields to seperate text files In reply to
Watts:

You helped me and you didn't even know it! IfFixed my problem!

I found a post that you made:

http://www.gossamer-threads.com/...overwrite%22;#176746

I had to change:

open (PASS, ">$other_pw_file") or &cgierr ("unable to open: $other_pw_file.\nReason: $!");

to

open (PASS, ">>$other_pw_file") or &cgierr ("unable to open: $other_pw_file.\nReason: $!");

Learned from your post that > overwrites the file in perl while >> appends the file in perl.

Thanks! I am set for now!