Gossamer Forum
Home : Products : DBMan : Customization :

Select field from external text file

Quote Reply
Select field from external text file
I am using a snippet of code found in the thread:

Thread reference: http://gossamer-threads.com/p/002323
Topic: Select in default.cfg OR second db ??
Yokhannan - March 10, 2000

What this code does in pull in a select list from an external file. The code is:

|;
open(SELECT,"pteam.txt") || die "Can't open file";
@pteam_list = (<SELECT> );
close (SELECT);
print qq|<select name="PTeam">|;
foreach (@pteam_list) {
chomp $_;
print qq| <option>$rec{'PTeam'}</option>|;
print "$_\n";
}
print qq|</select>

With a note to keep a blank line as the first line in the file.

This works great for adding the records into the database, but when I modify the record it shows the original selection prior to every other option in the file :)

Can someone help me by giving me suggestions on how I can modify the code to prevent this. The list I'm using is very long, so I'd like to keep it in an external text file. It will then be easier to keep this field updated if necessary.

Any help is greatly appreciated.

Lois

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Select field from external text file In reply to
Hey Lois, try this..

Code:
|;
open(SELECT,"pteam.txt") || die "Can't open file";
@pteam_list = (<SELECT> );
close (SELECT);
print qq|<select name="PTeam">|;
foreach (@pteam_list) {
chomp $_;

if ($_ eq $rec{'PTeam'}) {
print qq| <option selected>$_</option>|;
} else {
print qq| <option>$_</option>|;
}


print "$_\n";
}
print qq|</select>

chmod

Last edited by:

chmod: Feb 19, 2002, 1:57 AM
Quote Reply
Re: [chmod] Select field from external text file In reply to
chmod:

Thank you for the response. I tried the code but kept getting missing right curly bracket error message?

I tried adding another one prior to:

print qq|</select>

and then a few other places and still kept getting the error, which is strange cause it seems they should match.

Any ideas?

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Select field from external text file In reply to
Hmm, thats odd, the code definately doesn`t have any missing curlys, before I posted I tested it on my machine and it works fine..

Basically all I did was place the if else in there to find the selected option.

Do you use EditPlus it has a match braces function which is useful for finding missing braces.

chmod
Quote Reply
Re: [chmod] Select field from external text file In reply to
I use Professional File Editor to create my files.

I ran the script I use called brackets.cgi and checked all the files and there was no missing brackets with the original code I used.

This is very strange, but I'm hoping there is a solution to prevent the original field value from displaying prior to each selection from the external text file.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Select field from external text file In reply to
Hey Lois, can you give an example of what you mean by:

Quote:
to prevent the original field value from displaying prior to each selection

How is it displaying? Just curious...
Quote Reply
Re: [Watts] Select field from external text file In reply to
Ok, for example when I enter a record and choose a team called Hershey Bears when I go to modify the record and click on the dropdown select list I get:

Hershey BearsAdirondack IceHawks
Hershey BearsAlbany River Rats
Hershey BearsAmarillo Rattlers
Hershey BearsAnaheim Mighty Ducks
Hershey BearsAnchorage Aces
Hershey BearsArkansas RiverBlades
Hershey BearsAsheville Smoke
Hershey BearsAtlanta Thrashers
......

Instead of just:

Adirondack IceHawks
Albany River Rats
Amarillo Rattlers
Anaheim Mighty Ducks
Anchorage Aces
Arkansas RiverBlades
Asheville Smoke
Atlanta Thrashers
....

so the select display is including the original selection on each select line except the first which shows what was chosen from the add form. i.e. Hershey Bears

Very strange :)

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Select field from external text file In reply to
Since I'm strictly an "html kind of guy" this may not work, but I think I see the problem...

Try rearranging your lines to something like this:

Code:
|;
open(SELECT,"pteam.txt") || die "Can't open file";
@pteam_list = (<SELECT> );
close (SELECT);
print qq|<select name="PTeam">|;
print qq| <option>$rec{'PTeam'}</option>|;
foreach (@pteam_list) {
chomp $_;
print "$_\n";
}
print qq|</select>


The above may not work, but I can see what's wrong with regular code, the <option> tag (with the $rec{'PTeam'} field) is in the foreach loop so it's printing your db rec value for each line. Plus there is no line break between the option tag and the 'print $_' so your getting:

dbvaluetextfilevalue
dbvaluetextfilevalue

etc...

Good Luck!
Quote Reply
Re: [Watts] Select field from external text file In reply to
Code:
|;
open(SELECT,"pteam.txt") || die "Can't open file";
@pteam_list = (<SELECT> );
close (SELECT);
print qq|<select name="PTeam">|;
print qq| <option>$rec{'PTeam'}</option>|;
foreach (@pteam_list) {
chomp $_;
print "$_\n";
}
print qq|</select>

I don't see how that code can work at all. Surely:

print "$_\n";

...needs to be...

print "<option>$_\n";

You could get rid of the foreach loop altogether and do:

print map { "<option>$_" } @pteam_list;

...even better would be a while loop.

Last edited by:

RedRum: Feb 20, 2002, 8:08 AM
Quote Reply
Re: [RedRum] Select field from external text file In reply to
Thanks for the responses.

It works great now!!! Many thanks :)

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/

Last edited by:

LoisC: Feb 20, 2002, 8:26 PM
Quote Reply
Re: [LoisC] Select field from external text file In reply to
Hi LoisC, i would like to try this code. Do use this in the html.pl file ? Can you explain ?

thanks
Denny
Quote Reply
Re: [dstamos] Select field from external text file In reply to
Here is the notes I had for using this in one of my database:

External select field list

I put this into my html.pl file, then used a text file (1 option per line) and it has at last count over 1800 options. NO wait time while it lists them either.

in my CFG file I have the following under the database definition part code:

'PTeam' => [13,'alpha',40,255,1,'',''], # current team - external text file

Just treated as a regular text field, not as an option list. Works like a charm!

One thing to remember, the text file MUST have a blank line at the top of the text file else it will search using that name in the field.

In html_record_form use:

|;
open(SELECT,"pteam.txt") || die "Can't open file";
@pteam_list = (<SELECT> );
close (SELECT);
print qq|<select name="PTeam">|;
print qq|<option>$rec{'PTeam'}</option>|;

foreach (@pteam_list) {
chomp $_;
print "<option>$_\n";
}

print qq|</select>

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Select field from external text file In reply to
Won't that list the current value of the field twice in the select field? You may not notice if you've got 1800 choices, but it's one of those things that bother my obsessive-compulsive miind. :-) I hope you don't mind if I tweak it just a bit. It won't make much difference in the way it works.

I would create a subroutine in db.cgi:
Code:
sub build_select_field_from_external_file {
my ($column, $value, $file);

open (SELECT,"$file") || die "Can't open file";
@list = (<SELECT>);
close (SELECT);

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


Then, when you need the select field to appear in the form, use

print &build_select_field_from_external_file("fieldname","$rec{'fieldname'}","filename");

In Lois's case, this woule be

print &build_select_field_from_external_file("PTeam","$rec{'PTeam'}","pteam.txt");

It could be that, by going through all of the options to find the one that may already be selected, it'll slow things down when there are many options. In that case, Lois's original code would likely be preferable. But for those who don't have that many won't see a difference in speed. Also, it allows for multiple uses of the concept without having to repeat the code and it sorts the options alphabetically. I'm not sure that this code requires having a blank line at the top of the file. I haven't tried it out.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [LoisC] Select field from external text file In reply to
LoisC its works great! simple and effective.
thanks for your help.