Gossamer Forum
Home : Products : DBMan : Customization :

Text Output

Quote Reply
Text Output
Hi would someone have some idea how to do this, I have the following sub The sub html_printout at the end of this post sends the data to a text file How do I get the results of the sub average to output in the output file as well?

sub get_average {
# --------------------------------------------------------
# Get the "average" value of a field.
#

my $which_id = shift;
my $id_pos = 0;
my $rating_pos = 22;
my $which_db = $db_script_path . "/rating.db";
my $avg_count = 0; # Initialise the counter
my $avg_total = 0; # Initialise the total

open (DB, "<$which_db") or &cgierr("Unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

foreach $line (@lines) {
chomp ($line);
@tmp_record = split (/\Q$db_delim\E/o, $line);
if ($tmp_record[$id_pos] eq $which_id) {
$avg_count++; # Count up each record
$avg_total = $avg_total + $tmp_record[$rating_pos]; # Add rating to total
}
}
if ($avg_count > 0) {

$avg_total = int $avg_total / $avg_count; # Average the total by number of records
} else { $avg_total = 'Player not yet rated'; }

print $avg_total;

}

sub html_printout {
my (@hits) = @_;

&switch_to_rating;

%rec2 = &get_record($rec{'Playerid'});

&switch_to_player;


my ($numhits) = ($#hits+1) / ($#db_cols+1);

$output = qq|Firstname,Lastname,Address,City,ProvState,Country,Postal\n|;

for (0 .. $numhits - 1) {
%rec = &array_to_hash($_, @hits);
$output .="$rec{'Firstname'},$rec{'Lastname'},$rec{'Address'},$rec{'City'},$rec{'ProvState'},$rec{'Country'},$rec{'Postal'},&get_average($rec2{'Playerid'});\n;"}
open (TEXT, ">/www/home/proscouting.com/cgi-bin/hockey/whl/bwk/reports.db") or
&cgierr("error in writing text file. \nReason: $!");
if ($db_use_flock) {
flock(TEXT, 2) or &cgierr("unable to get exclusive lock on text
file.\nReason: $!");
}
print TEXT $output;
close TEXT;

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: Text File Saved</title>
</head>
<body bgcolor="ffffff">
<center>
<table border=0 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=600
align=center valign=top>
<tr><td colspan=2 bgcolor="c60000">
<FONT FACE="MS Sans Serif, arial,helvetica" size=-1 COLOR="#FFFFFF">
<b>$html_title: Text File Saved</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>Text File Saved
</b></font></center><P>
<$font>
The text file was successfully saved. <a href="/data/text.txt">View
File.</a>
<a href="ftp://ftp.proscouting.com/pub/download/wiz97e.exe">Label
Software for Microsoft Word.</a></FONT>
<P>|;
print qq|</td></tr>
</table></center></body></html>|;
}