Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

How to get a total from certain fields?

Quote Reply
How to get a total from certain fields?
Hello,

I have about 60 fields that either will contain a number 1 through 4, or they won't contain a number at all.

I'd like to be able to add up all of these fields to get a grand total of the numbers in them. Is this possible, I assume it is, could someone help me out on this (unless it's too time consuming, It's not that big of a deal to have, just would be nice if we did).

Data is stored in the main Links database

Columns are in the Links table (I assume, it's the same table where people would normally enter their link information)

Fields are named cardqty1 through cardqty55

Thanks for your time, it is greatly appreciated.
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Hi,

Are you just wanting to do this in the template? If so, should be as simple as doing:

<%set cardqty_total = cardqty1 + cardqty2 + cardqty3 + cardqty4 + cardqty5 + cardqty6 + cardqty7 + cardqty8 + cardqty9 + cardqty10 + cardqty11 + cardqty12 + cardqty13 + cardqty14 + cardqty15 + cardqty16 + cardqty17 + cardqty18 + cardqty19 + cardqty20 + cardqty21 + cardqty22 + cardqty23 + cardqty24 + cardqty25 + cardqty26 + cardqty27 + cardqty28 + cardqty29 + cardqty30 + cardqty31 + cardqty32 + cardqty33 + cardqty34 + cardqty35 + cardqty36 + cardqty37 + cardqty38 + cardqty39 + cardqty40 + cardqty41 + cardqty42 + cardqty43 + cardqty44 + cardqty45 + cardqty46 + cardqty47 + cardqty48 + cardqty49 + cardqty50 + cardqty51 + cardqty52 + cardqty53 + cardqty54 + cardqty55%>

..then, you will have <%cardqty_total%> as a tag (the total of them all added up)

Untested, but something like that should work.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
For some reason that just returns the first amount in the list, (cartqty1) instead of actually totaling them up.
since it prints to the screen the first fields quantity, could it be that the individual field names need to be encapsulated in something?

Yes it's just for display purposes, so it's just to put in the detailed.html template.

Last edited by:

Westin: Apr 29, 2009, 5:48 AM
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Mmm, try doing them as:

$cardqty50 + $cardqty51 + $cardqty52 + $cardqty53 + $cardqty54

..etc, and see if that helps.

Otherwise, just a case of passing them all to a global, and doing the addition in there.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
I guess they have to go as a global, adding the $ to the front of each one doesn't seem to do anything for me.
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Ok, NP - well, try this then:

do_count_numbers
Code:
sub {
my $total;
for (my $i=0; $i <= 54; $i++) {
print qq|working on $i, with value of : $_[$i] <br /> |;
if ($_[$i] =~ /^\d+$/) { $total += $_[$i]; }
}
return $total
}

..then call with:
Code:
<%set total_cardqty = do_count_numbers($cardqty1, $cardqty2, $cardqty3, $cardqty4, $cardqty5, $cardqty6, $cardqty7, $cardqty8, $cardqty9, $cardqty10, $cardqty11, $cardqty12, $cardqty13, $cardqty14, $cardqty15, $cardqty16, $cardqty17, $cardqty18, $cardqty19, $cardqty20, $cardqty21, $cardqty22, $cardqty23, $cardqty24, $cardqty25, $cardqty26, $cardqty27, $cardqty28, $cardqty29, $cardqty30, $cardqty31, $cardqty32, $cardqty33, $cardqty34, $cardqty35, $cardqty36, $cardqty37, $cardqty38, $cardqty39, $cardqty40, $cardqty41, $cardqty42, $cardqty43, $cardqty44, $cardqty45, $cardqty46, $cardqty47, $cardqty48, $cardqty49, $cardqty50, $cardqty51, $cardqty52, $cardqty53, $cardqty54, $cardqty55)%>
TOTAL WAS <%total_cardqty%>

Again, untested - but should work.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Apr 29, 2009, 7:58 AM
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Just appears blank where the total should be. Could it have something to do that not all those fields are going to always have a number in them?

What I mean is, that one one record it might have cardqty1 through cardqty10, and another detailed page another record might have cardqty1 through cardqty30 and so on and so on. No way to tell on each page until ti's loaded. Don't know if this makes a difference or not. But at the moment <%total_cardqty%> is just showing a blank spot.
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
What kind of values do they have? 12234 or 1234.33? It will only count for numbers (whole ones)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Please try the above edited global, and let me know what the output is (I've added some debugging in)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Copied and pasted as usual as to not make any mistakes.

Still just shows:
TOTAL WAS
and then blank.

Im working with whole numbers
1
2
3
4
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Mmm, not sure. If you want, send over GLinks admin details via email (in my signature), and I'll have a look for you tomorrow.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Anybody else have any clues on how to do the addition of the numbers inside of the template itself? Maybe we're just missing something simple.
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Did you try this one, to see what it prints out?

Code:
sub {
use Data::Dumper;
print "<pre>" . Dumper(@_) . "</pre>";
my $total;
for (my $i=0; $i <= 54; $i++) {
print qq|working on $i, with value of : $_[$i] <br /> |;
if ($_[$i] =~ /^\d+$/) { $total += $_[$i]; }
}
return $total
}

It should print out all the variables being passed in.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Template Global
do_count_numbers
Code:
sub {
use Data::Dumper;
print "<pre>" . Dumper(@_) . "</pre>";
my $total;
for (my $i=0; $i <= 54; $i++) {
print qq|working on $i, with value of : $_[$i] <br /> |;
if ($_[$i] =~ /^\d+$/) { $total += $_[$i]; }
}
return $total
}


Inside the detailed.html template:
Code:

<%set total_cardqty = do_count_numbers($cardqty1, $cardqty2, $cardqty3, $cardqty4, $cardqty5, $cardqty6, $cardqty7, $cardqty8, $cardqty9, $cardqty10, $cardqty11, $cardqty12, $cardqty13, $cardqty14, $cardqty15, $cardqty16, $cardqty17, $cardqty18, $cardqty19, $cardqty20, $cardqty21, $cardqty22, $cardqty23, $cardqty24, $cardqty25, $cardqty26, $cardqty27, $cardqty28, $cardqty29, $cardqty30, $cardqty31, $cardqty32, $cardqty33, $cardqty34, $cardqty35, $cardqty36, $cardqty37, $cardqty38, $cardqty39, $cardqty40, $cardqty41, $cardqty42, $cardqty43, $cardqty44, $cardqty45, $cardqty46, $cardqty47, $cardqty48, $cardqty49, $cardqty50, $cardqty51, $cardqty52, $cardqty53, $cardqty54, $cardqty55)%>
TOTAL WAS <%total_cardqty%>


Prints out on the detail pages:
TOTAL WAS

Last edited by:

Westin: May 11, 2009, 3:27 AM
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Can you send over GLinks login details, and I'll take a quick look for you. Gonna be something silly.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
email sent.
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Ok, after some debugging - looks like GLinks just didn't like the field name for some reason - weird. Renamed it to do_total_count, and seems fine now :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Andy, would it be much of a change to make it count NON whole numbers (1.33) etc?
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Just change:

Code:
if ($_[$i] =~ /^\d+$/) { $total += $_[$i]; }

..to:

Code:
if ($_[$i] =~ /^\d+\.?\d+?$/) { $total += $_[$i]; }

Untested, but should work.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Works fine thanks. Glad that you found out about how naming things sometimes causes them not to function. Had another situation where I named the global my_total_price and it wouldn't give me the total, renamed it to do_total_count2 and it worked fine for me. Strange stuff.
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Ya, maybe an issue with GT::Template - maybe GT could shed some light =) Adrian?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Another question about this.

As you may remember, I am pulling some prices out of another table with this tag: <%get_value_prices($cardname3)%> Which will display card3's average total from the prices table and works fine.

So now I want to get a total of all the average prices and have added this:

<%set averagetotal = do_total_count2($get_value_prices($cardname1), $get_value_prices($cardname2), $get_value_prices($cardname3))%>

Is that right? Can that actually work? It just comes up blank so Im thinking it can use it the way it's written, but if it can then I'll look elsewhere as to what the problem might be.

Thanks,

Update: Maybe I have to write a bunch of these: <%set avgprice1 = get_value_prices($cardname1)%>
and then use
<%set averagetotal = do_total_count2(avgprice1, avgprice2, avgprice3)%>

Seems like that would be a lot of work though to accomplish this but if that's right, then that's what I'd have to do.

Last edited by:

Westin: May 13, 2009, 12:46 AM
Quote Reply
Re: [Westin] How to get a total from certain fields? In reply to
Hi,

You can't call it like that. Would need something more like:
Code:
<%set tmp_value_1 = get_value_prices($cardname1)%>
<%set tmp_value_2 = get_value_prices($cardname2)%>
<%set tmp_value_3 = get_value_prices($cardname3)%>

<%set averagetotal = do_total_count2($tmp_value_1, $tmp_value_2, $tmp_value_3)%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to get a total from certain fields? In reply to
Perfect thank you.

Wasn't that big of a deal after all what with copy, paste and replace commands in my Notepad ++ program.