Calculation element with checkbox element

vaughan

Member
Hi

I have a calculation element plugin, it SUM 3 fileds:
field1=dropdown with 4 values
field2=multiselect checkbox with 4 values (each value=30)
field3=Checkbox with 1 value

I want to SUM this 3 fields with the user selections and works fine, but only when field2 choose only 1 value, if users select 2 or 3 values in checkbox (field2) only SUM 1 value

My calculation is
return '{tablename___field1_raw}'+'{tablename___field2_raw}'+'{tablename___field3_raw}';

How can I do to SUM the values in field2 when a user choose some values?

For example
field1=60
field2=30+30+30
field3=40
Total=190, but in my calculation SUM 130 (only SUM 1 selection in field2)

Thanks for your help

Best regards
 
Multiselect elements will be arrays, not single values. And in a placeholder, that will get represented as comma separated values.

So best to use the actual data array, $data['tablename___fieldname'], rather than relying on placeholders, which really can't represent multiple values in a way you can do math on them.

Something like ...

Code:
return $data['tablename___field1_raw'] + array_sum($data['tablename___field2_raw']) + data['tablename___field2_raw'];

NOTE - if you have "Calc on save only" set to No, you may have to do some extra coding, as when rendering the form (as opposed to submitting it) checkbox data is going to be a JSON representation, not an actual array, so ["1","2","3"], so you'd have to test what data type it is before operating on it.

-- hugh
 
Multiselect elements will be arrays, not single values. And in a placeholder, that will get represented as comma separated values.

So best to use the actual data array, $data['tablename___fieldname'], rather than relying on placeholders, which really can't represent multiple values in a way you can do math on them.

Something like ...

Code:
return $data['tablename___field1_raw'] + array_sum($data['tablename___field2_raw']) + data['tablename___field2_raw'];

NOTE - if you have "Calc on save only" set to No, you may have to do some extra coding, as when rendering the form (as opposed to submitting it) checkbox data is going to be a JSON representation, not an actual array, so ["1","2","3"], so you'd have to test what data type it is before operating on it.

-- hugh
thanks a lot!
I underastand the concept with array and I find the solution with a similar code.
thanks for your help
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top