Gossamer Forum
Home : Products : DBMan : Customization :

Add up two fields, then sort and list by sum

Quote Reply
Add up two fields, then sort and list by sum
Hi all. I have searched and searched through these forums but have not yet found what I am looking to do. Here's an example:

Let's say there is a database for quantities of fruit for different people. We have fields for OwnerName, TotalApples and TotalOranges. What I want is to have a page that adds up the TotalApples and TotalOranges for each OwnerName, then displays a list of OwnerName's sorted by TotalFruit (which is TotalApples + TotalOranges)

Is there a way to do this?

Thank you
Quote Reply
Re: [Dempsey] Add up two fields, then sort and list by sum In reply to
Probably, but I'm not sure I understand your database. What is held in each record? The number of apples and oranges in each orchard? Do owners have multiple orchards?

It often is easier to work out what's going on if you just give me the names of the actual fields you're using. :-)

This isn't something I can dash off in a minute, and probably will only be willing to give you a rough outline of what to do, once I understand exactly what you're trying to do.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Add up two fields, then sort and list by sum In reply to
Thanks a ton for your reply JPD.

Specifically my database is for hockey statistics.

I have a database file that is created by a seperate software program. Each row in the database represents a hockey player. Each player has a field for goals and a field for assists, and what I want to do is list all hockey players sorted by points (goals + assists).

Thank you very much for any help you can give me.
Quote Reply
Re: [Dempsey] Add up two fields, then sort and list by sum In reply to
If you just have one record per player, the easiest thing to do would be to add another field -- TotalPoints -- that adds TotalGoals and TotalAssists. You could have db.cgi do the addition in sub add_record and sub modify_record -- assuming that you modify each player's record after a game. Then it would just be a matter of sorting on the TotalPoints field. It would be a different matter if you added a record for each player after each game, with the number of goals and assists just for that game and then you wanted to do a total, which is what I thought you wanted at the beginning.

First, create another field -- TotalPoints. If you already have a .db file, you'll need to add a delimiter to the end of each record to accommodate the new field.

In sub add_record, after

Code:
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);


add

Code:

$in{'TotalPoints'} = $in{'TotalGoals'} + $in{'TotalAssists'};


substituting the actual names of your database fields, of course.

Add the same line to sub modify_record, just after

Code:

my ($status, $line, @lines, @data, $output, $found, $restricted);


Then sort on that field. You don't have to worry about putting the field in sub html_record or sub html_record_form at all, unless you want to. It'll be a completely internal thing.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Add up two fields, then sort and list by sum In reply to
Thanks a bunch. This should work. The only problem I see is that I have over 800 players in the database that would have to be updated daily to add the two fields together.

I suppose what I could do is make a special modify form that has all fields hidden and modifies all records at the same time. In theory then, I would just have to click one button in my browser on this special modify form to update TotalPoints for all players. Would this work?

Thanks again
Quote Reply
Re: [Dempsey] Add up two fields, then sort and list by sum In reply to
I have tried this and it works. Thanks! But as it stands I would have to go to the modify form and "list all", then go one-by-one through all 800+ players and modify them, which I can't do on a daily basis.

Is there a way to modify all records (Players, in my case) at the same time? I want to keep all values the same, except for adding TotalGoals and TotalAssists to get TotalPoints and just have everything as hidden fields. I just don't know how to modify all records at the same time, it currently gives me a radio box to select the records one by one.

Any help?

Thank you
Quote Reply
Re: [Dempsey] Add up two fields, then sort and list by sum In reply to
Ah, yes. It's much better to do things like this at the beginning. Give me a day or so. I'm kind of caught up in something right now that's filling my brain with php instead of Perl. Once I get it settled (like, tomorrow, with any luck), I'll be able to give you my full attention.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Add up two fields, then sort and list by sum In reply to
Absolutely. Thank you
Quote Reply
Re: [Dempsey] Add up two fields, then sort and list by sum In reply to
I am so sorry. I forgot all about this. Do you still need it?


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