Gossamer Forum
Home : Products : DBMan : Customization :

External Text File for Drop-down Box Options

Quote Reply
External Text File for Drop-down Box Options
I apologize if this question has been asked before. It has been some time since I've been here and reading messages.

In the configuration file, is there a way to pull-in a list of variables for a drop-down box? Fox example, the current configuration file reads:

%db_select_fields = (
Fieldname1 => 'Option1,Option2,Option3',
Fieldname2 => 'OptionA,OptionB,OptionC'
);

Would I be able to have an external text file consisting of the values that the configuration file would be able to read? On occassion I need to update the drop-down, which means i would have to open the configuration file, add the new option, save it, upload it, change rights, etc. By having an external file I would just update the one-liner (i.e. Option1,Option2,Option3,Option4, etc.) and upload it.

Thanks in advance!
Quote Reply
Re: [jmortimer] External Text File for Drop-down Box Options In reply to
It wouldn't be hard to write the code to do what you want, but there are easier ways to do it.

You don't need to change the permissions on the .cfg file when you upload it a second time. Once the file has permission, it's set, as long as you keep the same name. Also, you probably don't have to change the permission for the .cfg file anyway. The permission that's needed is 644 : rw- r-- r-- which is the default permission given to all files on every server I've used. It is possible that yours is different, though.

The easiest way to make changes to the .cfg file would be to install the Gossamer Threads Fileman utility. It's completely free and will allow you to edit your files online. No uploading necessary. Very easy.

If you still want to use an external file for the select field options, the best thing would be to have a separate file for each select field. So, if you have a select field called "Type", you would want to have a text file called "Type.txt". The contents of the file would just be the options for that field, one per line. Put the files in the same directory as DBMan.

Add the following to db.cgi:

Code:

sub build_select_field_from_file {
my ($column, $value) = @_;
my (@selectfields, $output);
$fieldfile = $db_script_path . "/" . $column . ".txt";
open (FIELD, "<$fieldfile") or &cgierr("error in building field. unable to open file: $fieldfile.\nReason: $!");
if ($db_use_flock) {
flock(FIELD, 2) or &cgierr("unable to get exclusive lock on $fieldfile.\nReason: $!");
}
LINE: while (<FIELD>) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
push (@selectfields, $line);
}
close FIELD; # automatically removes file lock
$output = qq|<SELECT NAME="$column"><OPTION>---|;
foreach $field (@selectfields) {
$field eq $value ?
($output .= "<OPTION SELECTED>$field\n") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";
return $output;
}



Where you want to print out your select field use print &build_select_field_from_file ("Fieldname", "$rec{'Fieldname'}");


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

Last edited by:

JPDeni: Dec 23, 2006, 3:11 PM
Quote Reply
Re: [JPDeni] External Text File for Drop-down Box Options In reply to
Thank you! I will have to give it a try.

Its been a while since I've had to modify the files so I must be confused. I thought I had to change the permissions. Thank you for clarifying that. Many times what I do is rename the "working" copy and upload the new file - this way if I have to revert back to something that works, I'll have a copy of it.
Quote Reply
Re: [jmortimer] External Text File for Drop-down Box Options In reply to
FWIW - I often rename the "working copy" and upload my changes. The only time I've had to re-do permissions is when I've renamed the db.cgi file.
Quote Reply
Re: [Watts] External Text File for Drop-down Box Options In reply to
I though I would try this mod rather than using another one that worked for me as it would work for several fields.

It seems to delete the file contents of the external file when I go to add a record ... any ideas?

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] External Text File for Drop-down Box Options In reply to
Looks like I put in the wrong arrow. Try < instead of > in the "open" line.

open (FIELD, "<$fieldfile")

etc.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] External Text File for Drop-down Box Options In reply to
That did the trick :)

Thank you very much !

Unoffical DBMan FAQ

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