Gossamer Forum
Home : Products : DBMan : Customization :

Adding totals subroutine/changing order

Quote Reply
Adding totals subroutine/changing order
Hello Everyone,

I've added a subroutine (found on this site) to my database that totals a data set. However, these totals are returned in alphabetical order and I like them to be ordered by the classifcation field in the database. Here are routines I have in the html.pl and db.cgi files.

HTML.pl subroutine
sub view_html_totals {
# --------------------------------------------------------

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: View Totals.</title>
</head>

<body bgcolor="#DDDDDD">
<FONT face="arial,verdana,helvetica" color="navy" size="-1">
<div align="left">
<p><br>
COUNT SUMMARY<br>
<p>

|;

&view_totals;
foreach $Species (sort keys %total) {
print qq|$Species - $total{$Species}<br>|;
}
print qq|<br>Total Bird Count = $grand_total|;
print qq|
</div>
</font>
</body>
</html>
|;
}

db.cgi subroutine

sub view_totals {
# --------------------------------------------------------

my ($status,@hits) = &query("view");
my ($numhits) = ($#hits+1) / ($#db_cols+1);

for (0 .. $numhits - 1) {
my (%rec) = &array_to_hash($_, @hits);
$total{$rec{'Species'}} += $rec{'Number'};
$grand_total += $rec{'Number'};
}
}

Also here is how it is called in the (layout code not included).

sub html_view_success {
# --------------------------------------------------------
Your search returned <b>$db_total_hits</b> matches.</font>

|;
if ($db_next_hits) {
print "<br><$font>Pages: $db_next_hits</font>";
}

# Go through each hit and convert the array to hash and send to
# html_record for printing.
print "<table>";
for (0 .. $numhits - 1) {
++$rec_count;
if ($rec_count%2) {
print "<tr>\n";
}
print "<td>";
&html_record (&array_to_hash($_, @hits));
print "</td>";
unless ($rec_count%2) {
print "</tr>\n";
}
}
# this section prints out a blank cell in case there are an odd number of records returned
unless ($rec_count%2) {
print "<td>&</td></tr>\n";
}
print "</table>";

if ($db_next_hits) {
print "<br><$font>Pages: $db_next_hits</font>";
}

print qq|
<p>
<table border=0 cellpadding=2 cellspacing=1 width="100%" valign=top>
<tr><td>

|;
&view_html_totals;
&html_short_search(); print qq|

I've also added the appropriate code in the sub main.

As I said ... it all works, however, the the data is returned alphabetically and I like the data order by the "Classification" field in the database.

Thanks so much,

Keith F. Saylor
Quote Reply
Re: [ksaylor] Adding totals subroutine/changing order In reply to
If you will always be sorting by the same field when viewing your database you could add in both:

sub html_view_search and sub html_view_failure

the following line to your forms:

<INPUT TYPE="hidden" name="sb" value="2">

value=" " would be the field number you want to sort by.

Or in case you may change your mind about the field you have chosen, define a variable for your search field.

In your .cfg file include:

$sort_field ='10'; ### global sort order for this database

then add your field to the subs above using:

<INPUT TYPE="hidden" name="sb" value="$sort_field">

Very easy to then modify the database sort order with one change. Use the global variable in whatever mods you need to include the sort field.

Other sorting options are available in the FAQ noted below in the section "Linking/Sorting"


Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Adding totals subroutine/changing order In reply to
Hello Lois,

Thanks so much for your reply. I am aware of the search options you referenced in your reply at the unofficial site. I had tried them before submitted my original question. See, they do not work. I think it is somehow related to the fact that there is a separate totals routine in the db.cgi file, although I am not sure. It will sort the orginal data by classfication but not the totals data. Do you have any thoughts on why?

Thanks so much,

Keith F. Saylor
Quote Reply
Re: [ksaylor] Adding totals subroutine/changing order In reply to
I am not sure I understand fully whether you are using the mod to get a grand total or are looking for subtotal according to classification.

I know you want the list sorted, but I would think the sort order alone would provide this, although it's not for you.

Have you looked at the other threads related to Reporting in the FAQ?
When I was checking out a few the one titled "Subtotalling Search Results" did provide a sort function. I'm not sure if this is what you are looking for.




Unoffical DBMan FAQ

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