JP Deni once wrote a small mod on how to import field-names from another database (http://webmagic.hypermart.net/dbman/multi12.txt).
I happen to use this mod quite often, and found it to have one disadvantage: You have to hard-code the list of fields that the other database has.
I now wrote a subroutine which gets that list of fields on the fly from the database configuration file. That way, if the field list happens to change, you always get the current version.
You can call this subroutine by passing on the name of the database setup file without the extension. For example, if I am currently using database db1 and want to access field names (and their values) from db2, I'd do this:
@db_cols = &get_db_cols("db2"); # get db2 field names
In the above mod from JPDeni, this line replaces the line starting with "@db_cols = ('Name', 'URL'".
Add the following subroutine somewhere to html.pl or db.cgi (db.pl):
----------------------------------
sub get_db_cols {
$configfile = "$_[0].cfg";
open(FILE, "<$configfile") || &cgierr("Cannot open $configfile.\n$!");
undef $/;
while (<FILE>)
{ if (/(%db_def\s+=\s+\(\s+)('.*?)(\))/s) { $fields = $2;}}
close(FILE);
while ($fields =~ /(\n')(\w+)(')/g) {push (@db_cols, $2); }
return (@db_cols);
}
---------------------------
Explanation:
What the code does is basically to open the cfg-file, look for the field list and extract the field names. It contains pattern searches based on how the field name list is written in the cfg-file: the assumption is that the field name list follows the string "%db_def = (" and is in turn followed by a closing bracket ")". Another assumption is that field-names are placed at the beginning of a new line, enclosed by single quotes. This is how the cfg-file comes "out of the box", so unless you've changed that, the sub should work fine.
I'd be glad to have suggestions to improve upon the above code (I'm not very confident in my coding skills!). Perhaps it could then be added to JPDeni's mod in the unofficial FAQ.
Cheers,
kellner
I happen to use this mod quite often, and found it to have one disadvantage: You have to hard-code the list of fields that the other database has.
I now wrote a subroutine which gets that list of fields on the fly from the database configuration file. That way, if the field list happens to change, you always get the current version.
You can call this subroutine by passing on the name of the database setup file without the extension. For example, if I am currently using database db1 and want to access field names (and their values) from db2, I'd do this:
@db_cols = &get_db_cols("db2"); # get db2 field names
In the above mod from JPDeni, this line replaces the line starting with "@db_cols = ('Name', 'URL'".
Add the following subroutine somewhere to html.pl or db.cgi (db.pl):
----------------------------------
sub get_db_cols {
$configfile = "$_[0].cfg";
open(FILE, "<$configfile") || &cgierr("Cannot open $configfile.\n$!");
undef $/;
while (<FILE>)
{ if (/(%db_def\s+=\s+\(\s+)('.*?)(\))/s) { $fields = $2;}}
close(FILE);
while ($fields =~ /(\n')(\w+)(')/g) {push (@db_cols, $2); }
return (@db_cols);
}
---------------------------
Explanation:
What the code does is basically to open the cfg-file, look for the field list and extract the field names. It contains pattern searches based on how the field name list is written in the cfg-file: the assumption is that the field name list follows the string "%db_def = (" and is in turn followed by a closing bracket ")". Another assumption is that field-names are placed at the beginning of a new line, enclosed by single quotes. This is how the cfg-file comes "out of the box", so unless you've changed that, the sub should work fine.
I'd be glad to have suggestions to improve upon the above code (I'm not very confident in my coding skills!). Perhaps it could then be added to JPDeni's mod in the unofficial FAQ.
Cheers,
kellner