Gossamer Forum
Home : Products : DBMan : Customization :

syntax question

Quote Reply
syntax question
i'm modifying the export to excel mod and want to change the section that creates csv records. i have defined a variable in cfg file called $db_export_fields (similar to $db_radio_fields, etc) that lists the fields i want to export. it worked fine. now i want to modify further so that if i haven't defined any fields to export, the export will export all the non-admin non-hidden fields per the original mod. i haven't figured out how to test whether the $db_export_fields is empty or not. right now, my condition must be evaluating as false (even though i have defined fields properly) because it is exporting all the non-hidden fields. here's my code
Code:

sub csv_record {
# --------------------------------------------------------
# writes record in CSV format
my (%rec) = @_; # Load any defaults to put in the VALUE field.
# ($db_auto_generate and print &build_html_record(%rec) and return);
my ($test);
@test = split (/\,/, $db_export_fields{$column});
my $num = 0;
if ($test[0]) {
foreach $test (%db_export_fields) {
if ($num > 0) {print CSVFILE "|";}
print CSVFILE qq|"$rec{$test}"|;
$num += 1;
}
print CSVFILE "\n";
}
else {
# Export all non-admin fields (admin form length = -2)
for (my $i =0; $i <= $#db_cols; $i++) {
if ($db_form_len{$db_cols[$i]} >= -1) {
if ($num > 0) {print CSVFILE "|";}
print CSVFILE qq|"$rec{$db_cols[$i]}"|;
$num += 1;
}
}
print CSVFILE "\n";
}
}

can you tell me what i'm doing wrong? i know the $db_export_fields variable is ok, because i haven't put the test in the csv_header section and it is exporting the header correctly (i commented out the default lines in the header).
Quote Reply
Re: [delicia] syntax question In reply to
i finally had time to look at this again and have it figured out. in cfg i have an array to list the fields i want included in csv file. then my export routines check the value of this array. if the array is empty or undefined, all non-admin fields are exported.

Code:

in cfg:
########if @db_export_fields is empty, all non-admin fields will be exported
@db_export_fields = (
Name,
Category,
Email,
Description
);


in html.pl:

sub csv_header {
# --------------------------------------------------------
# print field name headers for each column of data

my $num = 0;
my ($test);
$test = @db_export_fields;
if (!$test) {
for (my $i =0; $i <= $#db_cols; $i++) {
# Export all non-admin fields (admin form length = -2)
if ($db_form_len{$db_cols[$i]} >= -1) {
if ($num > 0) {print CSVFILE ",";}
print CSVFILE qq|$db_cols[$i]|;
$num += 1;
}
}
}
else {
for (my $i =0; $i <= $test; $i++) {
if ($num > 0) {print CSVFILE ",";}
print CSVFILE qq|"$db_export_fields[$i]"|;
$num += 1;
}
}
print CSVFILE "\n";
}
sub csv_record {
# --------------------------------------------------------
# writes record in CSV format
my (%rec) = @_; # Load any defaults to put in the VALUE field.
# ($db_auto_generate and print &build_html_record(%rec) and return);
my $num = 0;
my ($test);
$test = @db_export_fields;
if (!$test) {
# Export all non-admin fields (admin form length = -2)
for (my $i =0; $i <= $#db_cols; $i++) {
if ($db_form_len{$db_cols[$i]} >= -1) {
if ($num > 0) {print CSVFILE ",";}
print CSVFILE qq|"$rec{$db_cols[$i]}"|;
$num += 1;
}
}
print CSVFILE "\n";
}
else {
for (my $i =0; $i <= $test; $i++) {
if ($num > 0) {print CSVFILE ",";}
print CSVFILE qq|"$rec{$db_export_fields[$i]}"|;
$num += 1;
}
print CSVFILE "\n";
}
}
Code: