[SOLVED] Add several fields value to another one

4 years 11 months ago - 4 years 11 months ago #312380 by agaudin
Hello
I am currently creating a register form for a non benefit meeting ; I would like to let people preorder meals. I created radio buttons fields like
cb_meal1 : yes (value = 15) no (value = 0)
cb_meal2 : yes (value = 15) no (value = 0)
and so on.

I created also a cb_total_amount field, which I try to fullfil with the summ of checked meals. I tried to do it in several ways :

1/ Using the AutoAction "FIELD" : but the cb_mealN fields values are considerated as text, not numbers, so the calculation [cb_meal1]+[cb_meal2] gives logically "15 + 0" as an output
2/ Using the AutoAction "CODE" / php : but I am not sure on how to do (php code + value settings and so on)

Any idea ? I am sure that someone already made that kind of things !

Thanx by advance !
Best regards,
Antoine

Please Log in to join the conversation.

4 years 11 months ago #312394 by krileon
Replied by krileon on topic Add several fields value to another one
Do you need the values stored in the database so they can be searched? If not you could just use a Code field then add them together using PHP for display purposes. Example as follows.

return (int) '[cb_meal1]' + (int) '[cb_meal2]';

If you need them stored then ensure cb_total_amount is an integer field and use the Field action in CB Auto Actions acting on the onAfterUserRegistration, onAfterNewUser, onAfterUpdateUser, and onAfterUserUpdate triggers. Next you'll need to toggle on Format Functions under Parameters. Then the following should work.

Field: cb_total_amount
Operator: Set
Value: [cb:parse function="math"][cb_meal1] + [cb_meal2][/cb:parse]

The math format function allows you to use math operators to add, subtract, multiply, and divide.


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.
The following user(s) said Thank You: agaudin

Please Log in to join the conversation.

4 years 11 months ago #312399 by agaudin

krileon wrote: If you need them stored then ensure cb_total_amount is an integer field and use the Field action in CB Auto Actions acting on the onAfterUserRegistration, onAfterNewUser, onAfterUpdateUser, and onAfterUserUpdate triggers. Next you'll need to toggle on Format Functions under Parameters. Then the following should work.
Field: cb_total_amount
Operator: Set
Value: [cb:parse function="math"][cb_meal1] + [cb_meal2][/cb:parse]
The math format function allows you to use math operators to add, subtract, multiply, and divide.

Dear Krileon,
Thanx a lot ! As usual, you got it directly.
I tried exactly as you said EXCEPT I toogled on Format under Conditions, NOT Parameters (I didn't get exactly the difference - yours extensions are that much powerfull that we can do everythin we can imagin but it is difficult sometimes to know what settings to consider because of documentation... BUT your support is always fast and efficient - briliant !)
Thanks againt
Best regards
Antoine
The following user(s) said Thank You: krileon

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.189 seconds

Facebook Twitter LinkedIn