Racmitch
Member
I wanted to post this for all those who maybe looking for a simple math calculation for the fabrik calc plug-in. Hugh helped me understand how it worked and asked if I could share this in the forum for those who would benefit from the information. So here it goes.
I wanted to be able to have fields accept (in this case) not just numbers but also money values to sum and create a total, in the four basic math equations ( addition, subtraction, multiplication and division. ) but wasn't sure how to go about it.
here are the steps, if i got them wrong or out of sequence please feel free to let me know.
create the field in which your number values are added(these are the fields to be summed by the calc plug-in *note* this does not include dropdown lists, just straight up "field" elements.)
1. create your "field elements" to except a number value
2.make sure in the element "formatting" section you select "yes" to number format.
save/close
3. for the total field select "calc" as your plugin
4.You must do 2 things for sure and a third depending on how you want the calculation to work. you must tell ajax which "fields to" observe and you must enter the math coding to make the fields perform the task. The 3rd one I used was to click on ajax calculation to "yes". This does the calc real time as you are adding the values.
So for the first one "Ajax observe fields" you add the fields that are to be calculated like this
{full_element___name} ex: {calc_test___test_1} comma "," separate the list. it should look like this.
{calc_test___test_1},
{calc_test___test_2},
{calc_test___test_3}
add all the field in which a value will be evaluated to create your sum.
Then you need to add the code that fabrik hands off to php to do the math in the "calculation" box.
for add, subtract and multiply you can add this string replacing the factitious element names with yours(this one is for addition)
using (real) will calculate decimals for money calculations, you can use (int) for just whole numbers. This works the same way for multiplication as well but replace the "+" with "*"
*note* add "_raw" to the end of the element names being evaluated
Subtraction is basically the same thing except you(or at least I did) have to invert your element order to not get a negative number. ex: if {calc_test___test_1_raw}'s value is 6 and i'm subtracting from it from {calc_test___test_2_raw} who's value is 3 i want {calc_test___test_1_raw} last in the evaluation order so I get "3 from 6 is 3", if left in the same order as your addition formula it would act like "6 from 3 is -3
Division is a bit more tricky as php with throw a syntax error if a field is left blank, so we need to create some code to tell php to treat all unpopulated fields as "0" our string would look like this with the additonal code
it is best practice to add this line of code whether you will have "not required" fields in your calculation or not to avoid a syntax error. Place the "+,-, or * symbols with "/" for division. Also as with subtraction, invert your calculation order to not get negative number .
Hugh will add below the formula for making your totals , total, ex: say you have a calculation that adds numbers and calculates a Gross Total, and a calculation that creates Expense Total and we need these two "totals" to calculate into a "Net Total" .
There you go my friend, thank you so much for all your help, I wish you a safe and successful procedure, and a quick and full recovery!!! Fabrik ROCKS!!!!
I wanted to be able to have fields accept (in this case) not just numbers but also money values to sum and create a total, in the four basic math equations ( addition, subtraction, multiplication and division. ) but wasn't sure how to go about it.
here are the steps, if i got them wrong or out of sequence please feel free to let me know.
create the field in which your number values are added(these are the fields to be summed by the calc plug-in *note* this does not include dropdown lists, just straight up "field" elements.)
1. create your "field elements" to except a number value
2.make sure in the element "formatting" section you select "yes" to number format.
save/close
3. for the total field select "calc" as your plugin
4.You must do 2 things for sure and a third depending on how you want the calculation to work. you must tell ajax which "fields to" observe and you must enter the math coding to make the fields perform the task. The 3rd one I used was to click on ajax calculation to "yes". This does the calc real time as you are adding the values.
So for the first one "Ajax observe fields" you add the fields that are to be calculated like this
{full_element___name} ex: {calc_test___test_1} comma "," separate the list. it should look like this.
{calc_test___test_1},
{calc_test___test_2},
{calc_test___test_3}
add all the field in which a value will be evaluated to create your sum.
Then you need to add the code that fabrik hands off to php to do the math in the "calculation" box.
for add, subtract and multiply you can add this string replacing the factitious element names with yours(this one is for addition)
PHP:
return (real)'{calc_test___test_1_raw}' + (real)'{calc_test___test_2_raw} + (real)'{calc_test___test_3_raw}';
using (real) will calculate decimals for money calculations, you can use (int) for just whole numbers. This works the same way for multiplication as well but replace the "+" with "*"
PHP:
return (real)'{calc_test___test_1_raw}' * (real)'{calc_test___test_2_raw}' * (real)'{calc_test___test_3_raw}';
*note* add "_raw" to the end of the element names being evaluated
Subtraction is basically the same thing except you(or at least I did) have to invert your element order to not get a negative number. ex: if {calc_test___test_1_raw}'s value is 6 and i'm subtracting from it from {calc_test___test_2_raw} who's value is 3 i want {calc_test___test_1_raw} last in the evaluation order so I get "3 from 6 is 3", if left in the same order as your addition formula it would act like "6 from 3 is -3
PHP:
return(real) '{calc_test___test_2_raw}' - (real)'{calc_test___test_3_raw}' - (real)'{calc_test___test_1_raw}';
PHP:
if ((real)'{calc_test___test_1_raw}' == 0 or (real)'{calc_test___test_2_raw}' == 0) {
return 0;
}
return (real)'{calc_test___test_1_raw}' / (real)'{calc_test___test_2_raw}';
Hugh will add below the formula for making your totals , total, ex: say you have a calculation that adds numbers and calculates a Gross Total, and a calculation that creates Expense Total and we need these two "totals" to calculate into a "Net Total" .
There you go my friend, thank you so much for all your help, I wish you a safe and successful procedure, and a quick and full recovery!!! Fabrik ROCKS!!!!