simple math calculations with the calc plug-in

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)

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}';
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
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}';
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!!!!
 
  • Like
Reactions: rob
Thanks for summarizing our Skype session, hopefulyl that will help someone else. Note, I edited your post corrected a couple of things (you were missing a few '), and also put PHP tags around your code examples. When putting PHP in posts, it makes it much more readable if you use the little {} button at the top of the editor, which gives you a Insert Code popup, where you can select PHP as the language, and then your code with get prettyfied.

One thing to note - you shouldn't have to specify the element names to use as triggers for the AJAX calculation (the "triggers"), for any element placeholders which you use in your calc. We will automagically add 'onChange' triggers for any {table___foo} placeholders you use in the PHP for your code. It doesn't hurt to specify them in that box, but you shouldn't have to. In theory, that box is just for specifying additional elements you want to use as a triggers, which aren't actually used in the calc.

Anyway ...

So ... the issue with then creating a "Grand Total" from your "Sub Total" calc elements is, youc can't (well, shouldn't) use calc elements within other calc elements. So if {calc_test___subtotal_1} and {calc_test___subtotal_2} are two calc elements which sum various other elements on the form, although it would seem natural t be able to then just add those together in your calc_test___total element, you can't. It's a chicken and egg thing. When you form is submitted, Fabrik will run those calcs again in PHP before storing them ... and there is no guarrantee as to which order they will be calculated in. So you may very well end up with a total of 0, because that calc got run ebfore the two subtotals. Fabrik just has no way of knowing what order to run calcs in, to support "nesting" of calcs.

So ... although it's tedious, you have to re-perform any math you do in the subtotal calcs, in the total calc. So if your subtotal1 was test_1 + test_2, and your subtotal2 was test_3 * test_4, you'd have to do the total like this ...

PHP:
//redo subtotal 1, assign it to a variable
$subtotal1 = (real)'{calc_test___test_1}' + (real)'{calc_test___test_2}';
 
//redo subtotal 2, assign it to a variable
$subtotal2 = (real)'{calc_test___test_3}' * (real)'{calc_test___test_4}';
 
//return the total
return $subtotal1 + $subtotal2;

Note the use of $variables to temporarily store results in. Variable names can be (pretty much) anything, although it's safest to start it with a letter, and you can only user letters, numbers, and the _ character. No spaces.

Yeah, its kind of tedious, and one day we may add some kind of ordering directive for the calc element, so you can tell us what order you want them run in, to allow you to do it the easy way, like ....

PHP:
return (real)'{calc_test___tubtotal1}' + (real)'{calc_test___subtotal2}';

... but for now ... it is what it is.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top