Add up total of custom field values
Posted: 03 April 2007 05:32 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  235
Joined  01-14-2006

Hi, I have a custom field called ‘points’. Each user uses a pulldown menu to select a number from 1 to 10. Users can publish as many entries/points as they like.
 
I’d like to know how to add up the numbers entered by a particular user to give them a ‘total’ for all ‘points’ entered into the weblog..
 
If I knew some php I’m sure it would be easy smile.. can anyone help pls..
many thanks,
Iain.

 Signature 

Weblog | New Zealand Web Design Agency

Profile
 
 
Posted: 03 April 2007 07:15 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  235
Joined  01-14-2006

hmm, after some reading (php for dummies lol) i found some code that I’d thought would work:
 

<?php
$a
= array( {exp:weblog:entries weblog="cpd_points" status="Approved" username="CURRENT_USER"}{cpd_points}, {/exp:weblog:entries} );
echo
"sum(a) = " . array_sum($a) . "\n";
?>


But all I get is a blank page… I’ve tried enabling php on input and output..?..
I gotto go to bed… gulp

 Signature 

Weblog | New Zealand Web Design Agency

Profile
 
 
Posted: 03 April 2007 02:18 PM   [ Ignore ]   [ # 2 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23751
Joined  05-20-2002

Hm- I think I’d use a query- a lot leaner for something like this.  But going with this- what’s the result of just:

{exp:weblog:entries weblog="cpd_points" status="Approved" username="CURRENT_USER"}{cpd_points}, {/exp:weblog:entries}


And if this was going to work?  PHP would definitely need to be parsed on output.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 03 April 2007 04:26 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  235
Joined  01-14-2006

Hi Robin,
The output is something like:
5, 8, 1, 10, 15, 18, 16,
thanks,

Iain.

 Signature 

Weblog | New Zealand Web Design Agency

Profile
 
 
Posted: 04 April 2007 01:56 PM   [ Ignore ]   [ # 4 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23751
Joined  05-20-2002

Hm- well the code works when I hard code it.  So while I’d still go with a query, try:

<?php
$a
= array( {exp:weblog:entries weblog="cpd_points" rdf="off" status="Approved" username="CURRENT_USER"}{cpd_points}, {/exp:weblog:entries} );
echo
"sum(a) = " . array_sum($a) . "\n";
?>


It will have to be parsed on output for sure.  If that doesn’t work- put just the weblog tag in a test template again- this time, look at the ‘reveal code’- make sure nothing else is getting in there- such as spaces, etc.

Don’t really have a good way to test this myself, so I’m winging it a bit.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 13 December 2008 09:25 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  555
Joined  10-24-2005

Thanks Robin.  I took your code and turned it into a query.  I may try to turn it into a plugin.

<?php
            $total_amount
= array( {exp:query backspace="1" sql="SELECT exp_weblog_titles.entry_id, exp_weblog_data.entry_id, exp_weblog_data.field_id_21, exp_weblog_titles.author_id, exp_weblog_titles.status FROM exp_weblog_titles, exp_weblog_data WHERE exp_weblog_titles.entry_id = exp_weblog_data.entry_id AND exp_weblog_titles.author_id = {member_id} AND exp_weblog_titles.status = 'Accepted'"}{field_id_21},{/exp:query} );
            echo
"$" . array_sum($total_amount) . "\n";
            
?>

If my query can be more efficient, let me know:)

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 66475 Total Logged-in Users: 47
Total Topics: 84879 Total Anonymous Users: 23
Total Replies: 455407 Total Guests: 224
Total Posts: 540286    
Members ( View Memberlist )