Gossamer Forum
Home : Products : DBMan : Customization :

display field from external file

Quote Reply
display field from external file
ok, i don't have time today to work on the other problem, but now have a different problem, more urgent!

i'm using build_external_select to build a select list from db2 when adding records to db1. it's working fine. i further refined it to display the name (2nd field) in the file while saving the key (1st field). now my problem: in html_record, i need to look up the value of the field in the external database to display the name (and some other fields) instead of the key that was saved in db1.

i tried creating a version of sub get_record and calling it in html_record_long, but cannot get it to work. i've looked at FAQ and can't find anything except the relational database mod. i don't want to switch to the other database; i just want to lookup something for display in a new variable (perhaps $rec2). i figure it isn't necessary to switch since the build_external_select works without switching. this is what i tried:


Code:


%rec2 = &switch_to_places($rec{'Placeservice'}); ... print qq|
<TR><TD ALIGN="Right" VALIGN="TOP" WIDTH="20%"><p>Service:</p></TD>
<TD WIDTH="80%" VALIGN="TOP" ><p>&nbsp;$rec{'Dateservice'}<br>&nbsp;$rec{'Timeservice'}<br>&nbsp;$rec{'Placeservice'}&nbsp;$rec2[1]</p></TD></TR>
|;


i'm sure i'm not the first to do this. can you point me in the right direction? thanks!
Quote Reply
Re: [delicia] display field from external file In reply to
See if this helps you any... it grabs stuff from another db file. I used it to display the "Company" and "Branch" from another db file by sticking the values into $UCompany and $UBranch. You have to know which field is which (starting with 0). In this example the ID was field 9, and company was field 1 and Branch was field 2. (The key was field 0).

Code:
$db_name_list = 'data.db'; #name of other db file
open (OTHER, "<$db_name_list") or &cgierr("error!"); #open file or return error if not able
if ($db_use_flock) { flock(OTHER, 1); } #temporarily lock other file
while (<OTHER>) { #begin looping thru other file line by line
next if /^$/; #skip blank lines
next if /^#/; #skip comment lines(?)
chomp; #remove line breaks
@data = &split_decode($_); #use sub to convert break commands and delimiters
if ($data[9] eq "$rec{'UserID'}") { #field position (starts with zero) to match
$UCompany = $data[1]; #field position of data from line w/matching
$UBranch= $data[2]; #field position of data from line w/matching
}
}
Quote Reply
Re: [Watts] display field from external file In reply to
as you were posting this, i found a thread in your faq "displaying names associated with id's from another db" in multiple category. (btw, thanks for changing to let us save login info!)

i am having the problem the guy mentioned in that thread where it is displaying the key twice, instead of pulling the other field. see http://www.gossamer-threads.com/...orum.cgi?post=199024

i need to lookup info for three different fields in my html and bring back several fields of info (name, address, phone, website). all three fields use same external db for info.

i just read all the docs in the faq's under learning, but still don't understand difference between array and hash. can i use the code you provided above in a subroutine? will i need a hash? how do i send back several values from a subroutine without array or hash?

Last edited by:

delicia: May 17, 2004, 2:26 PM
Quote Reply
Re: [Watts] display field from external file In reply to
guess my previous reply didn't make sense -- i thought you were lois!

you mention the key field is 0 -- does that matter? i'm guessing from your code that the userid field is what you're matching from the first database and it occupies position 9 in the second?

here's my code:
Code:

sub getName {
# --------------------------------------------------------
my ($theID) = $_[0];
my ($db_KEY_field)=0; #<-Change this to fit your DB
open (OTHER, "<$db_other_file_name") or &cgierr("error in sub getName.... unable to open file $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <OTHER>;
close OTHER;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($data[$db_KEY_field] eq $theID) {
$theID=$data[1];
last; # <- found stop processing
}
return $theID; }
}
in the second db, i'm matching field 0. the data i want to retrieve is field 1. seems no matter what i change, i just get the original value of $theID. why isn't $theID=$data[1] changing the value to the value in field 1?

Last edited by:

delicia: May 17, 2004, 3:34 PM
Quote Reply
Re: [delicia] display field from external file In reply to
Not sure... just for grins try something like this:

if ($data[$db_KEY_field] eq $theID) {
$theID="";
$theID=$data[1];
last; # <- found stop processing
}

or maybe even doing:
my $theID;
$theID=$data[1];

to "zero out" the variable. I think it is getting the value set earlier on and then doesn't get reset during the if/then part.
Quote Reply
Re: [Watts] display field from external file In reply to
neither worked, but you got me motivated again. i finally got it to pass the right info by changing the return statement to return $data[1]
oops, spoke too quickly. it returned the proper field but for the wrong record. i was looking for record ID = 2 and wanted field 1. it sent record ID = 1 (the first one in the db file) but at least sent the right field...
i think it may not be properly matching the key values for some reason. i don't know how to determine if one is stored as number and one as string?

Last edited by:

delicia: May 17, 2004, 4:33 PM
Quote Reply
Re: [delicia] display field from external file In reply to
finally figured it out and the clue was in the original thread. the guy said it needed an extra closing curly bracket after last which of course causes error. but he's right that it needs it. so i added it and deleted one after the return statement and it is now pulling the correct info!