Skip to Content Skip to Menu

🌟Discover the Joomla AI Plugin that wrote its own story! - CB Editor Assistant 1.1now for Joomla 3, 4 & 5!
✍️ 5-Day Free Trial, then 🎯 save up to 30% with our 🛍️ Intro Offer (First 50 users, ends Dec. 31)
🌲 Merry Christmas! Great savings on Professional and Developer Memberships! Get 25% off now with code XMAS-2024!

Calculate the SUM value of multiple user fields?

  • jcrimmel
  • jcrimmel
  • OFFLINE
  • Posts: 44
  • Thanks: 9
  • Karma: 2
1 year 1 month ago - 1 year 1 month ago #335984 by jcrimmel
What would be the best way to SUM fields together? What I'm trying to figure out is how to add multiple fields for the user together.

If I had the 3 user fields below:
cb_uservalue1   with a stored value of 100
cb_uservalue2   with a stored value of 200
cb_uservalue3   with a stored value of 300

How would I display a field that essentially takes the SUM (cb_uservalue1),(cb_uservalue2),(cb_uservalue3) and stores/displays a value of 600 and updates with any change of the cb_uservalue field?

I do really well building and working inside of Community Builder, but my PHP, SQL, and coding skill is only a very marginal step above illiterate.

I would be grateful to learn how to accomplish the above, and ecstatic to learn how to do that "live" as the fields are changed on screen.... but one step at a time i guess. 

 
Last edit: 1 year 1 month ago by jcrimmel.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48630
  • Thanks: 8307
  • Karma: 1446
1 year 1 month ago #335993 by krileon
Do you really need the total to be stored in the database because if not CB Code Field with some PHP would be the easiest way. Example as follows.

Code Field:
Code:
return ( $user->getInt( 'cb_uservalue1', 0 ) + $user->getInt( 'cb_uservalue2', 0 ) + $user->getInt( 'cb_uservalue3', 0 ) );

If you need it stored in the database you have a couple of options. You could use CB Auto Actions and a Field action exclusively for this as there's a Format Function (see the Parameters tab of an auto action) called Math that can handle adding the fields together while the Field action stores the result into another field.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • jcrimmel
  • jcrimmel
  • OFFLINE
  • Posts: 44
  • Thanks: 9
  • Karma: 2
1 year 1 month ago - 1 year 1 month ago #336010 by jcrimmel
I was able to implement the Code Field and php you suggested and it works perfectly to display the SUM. I was able to use it in multiple areas and even adjusted to code to do some different math here and there, thank you.

However, I do have one instance where I need that SUM result to be shown as a field in a downloaded report. I have users downloading reports returning database columns to a csv, and without the SUM being stored as its own field in the database, the SUM result is not shown in the downloaded report.

I can tell that Auto Action could be pretty powerful, but for the life of me, I can not figure out how to use it. 
Last edit: 1 year 1 month ago by jcrimmel.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48630
  • Thanks: 8307
  • Karma: 1446
1 year 1 month ago #336038 by krileon
How are you generating these reports? If using a Joomla extension for that it might be possible to get substitutions to run against the export so you can just substitute in the code field. If the extension has you just supply the SQL then you can sum the fields together directly in your SQL.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • jcrimmel
  • jcrimmel
  • OFFLINE
  • Posts: 44
  • Thanks: 9
  • Karma: 2
1 year 3 weeks ago - 1 year 3 weeks ago #336198 by jcrimmel
I am using a 3rd party extension that is sql based. I'd love to find a way to inside CB, because I'm more familiar with that. 

In my CB field "cb_amicode", which sums the fields for display (which works great), I tried adding the following code which would also store the value in another field "cb_amitest", before displaying the sum result ... but not luck with that working.:
Code:
// Update the value of "cb_amitest" with the calculated result $user->set("cb_amitest", $result);
 

I don't think I can use the AutoAction>Field Type because the math is a little more than just adding two fields. Really it's adding 1 group of fields and then adding 2% of the result of another group that was previously summed. It works great in PHP code to display it, but seems AutoAction>Field Type wouldn't handle the more complex code that the Code field can handle.

I'm going to work with it tonight with ChatGPT to try it get some PHP code for my field that is displying it to also ?POST? or ?SET? it into another field. I don't see why PHP couldn't do that...just my limited experience requires me the lean on A.I. it help code it.
Last edit: 1 year 3 weeks ago by jcrimmel.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48630
  • Thanks: 8307
  • Karma: 1446
1 year 3 weeks ago #336200 by krileon

I am using a 3rd party extension that is sql based.

I suggest just doing the math in the SQL then. Using my example provided above this is as simple as the following.
Code:
SELECT ( `cb_uservalue1` + `cb_uservalue2` + `cb_uservalue3` ) FROM `jos_comprofiler`

I don't think I can use the AutoAction>Field Type because the math is a little more than just adding two fields. Really it's adding 1 group of fields and then adding 2% of the result of another group that was previously summed. It works great in PHP code to display it, but seems AutoAction>Field Type wouldn't handle the more complex code that the Code field can handle.

You don't put the code in CB Auto Actions. You keep it in your code field. You then use CB Auto Actions to substitute that code field into a field with storage. You shouldn't really need to do this. The best approach is to format the data how you need in your reporting extension and since that extension is SQL based that really shouldn't be a problem.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum