Haw to sum a join reaping group

keianrad

Member
There is a repeat group including a database join element as name of service {app_contracts_37_repeat___service} and an cascading element for showing service fee {app_contracts_37_repeat___service_fee}.

I need to calculate sum of repeated cascading elements. I used code listed below but it doesn't work

return array_sum($_POST['join'][32]['app_contracts_37_repeat___service_fee_raw']);

Ajax calculation: Yes
Ajax observe fields: {app_contracts_37_repeat___service}

And also group of those elements is different with calc element's group.
 
Last edited:
You must have gotten that code from a very old post. If you found it in the wiki, let me know. We haven't used that ['join'][X] structure for repeat data since 3.0 on J! 2.5.

Repeat groups are now stored like any other element, except the element's entry in the data array is an array, with as many entries as there are repeats.

So in a calc, $data['app_contracts_37_repeat___service_fee_raw'] will be an array.

-- hugh
 
I found it in this link in the forum.

I used your code but it doesn't work yet. It is showing "Loading...".

return array_sum $data['app_contracts_37_repeat___service_fee_raw'];
 
Well, that isn't syntactically correct. Put parens around the arg to array_sum, as per your first example.

-- hugh
 
You'll have to debug it. Once you have loaded the form, put ...

var_dump($data);exit;

... as the first line of the calc. Then trigger a change, and look at the Network tab in dev tools, you should see the AJAX call fire off to do the calc. Look at the "response". Make sure app_contracts_37_repeat___service_fee_raw is an array in the dumped data.

Or point me at it (is this your SSA site?).

-- hugh
 
Thank you for your help. perfect sum is working, for deleting I updated cals.js file from github but when I delete a row, it doesn't effect on the total. I clean the cash also but it is same with before updating.
 
Well, you shouldn't really cherry pick from github, see the wiki page. Upload everything (back up your site first, off course).

In this case it should be safe to just update those two files (the uncompressed and compressed), but I just checked your form page, and the calc.js hasn't changed, doesn't have the new code in it.

-- hugh
 
I used this code in another form but it doesn't work!?

Site: SSA
Form Name: Invoice
Element Name: Sub Total

Code:
return array_sum (FabrikWorker::JSONtoData($data['app_invoices_112_repeat___amount'], true));
 
Looks like you added that before you loaded the form, not (as per the instruction) after you have loaded the form.

The calc runs when loading the form, as well as when you modify element on the form (for the AJAX calc). So if you add that 'exit' before loading the form, all you'll see is the null data for the empty form being built.

-- hugh
 
I understood that this code doesn't work in order to sum of group calc element!!!

When this code using in a group field element, it works perfect. but it doesn't work in group calc element.

Code:
return array_sum (FabrikWorker::JSONtoData($data['app_orders___fee'], true));
 
Which element is this code in? Is it app_invoices___sub_total? Which is trying to sum app_orders___fee?

If so, that won't work. You can't use the value of a calc element in another calc element. It's a chicken and egg situation.

What you have to do is is repeat the entire calculation.

I was logged in to your site and working on a solution, but your site locked me out again. My IP is currently 75.76.16.158.

-- hugh
 
Yes, the element is "app_invoices___sub_total" which is trying to sum app_orders___fee.
Form Name: invoice

I have some other calc elements on that form which are working without any problem such as "Remaining" element that is calculating two calc elements values!

I asked to my host support to add your IP to the white list. It should be solved.

And also there are two joins in the invoice form "orders" and "payments" . I need to update invoice values when a new record is added or updated in one of these join lists.

Your IP added to the white list.
 
I have some other calc elements on that form which are working without any problem such as "Remaining" element that is calculating two calc elements values!

That's the problem. It's not consistent. Think about it. If calc1 needs the result of calc2, then Fabrik has to run calc2 first. But Fabrik has no way of knowing which of your calcs rely on others, and hence the order you want them to run in. We just run them in some arbitrary order, with no guarantee of what that order is going to be. So there's a 50/50 chance that calc2 runs first, and hence that calc1 will work as expected. If calc1 happens to run first, then it won't work.

This has always been a restriction on the calc element, which I thought I had documented in the Wiki, but apparently I didn't. So I've added that restriction in the "Import Notes" at:

http://fabrikar.com/forums/index.php?wiki/calculation-element/

So if you need the value of another calc, you need to redo the calculation. So if calc2 did ...

return (int)'{mytable___field1}' + (int)'{mytable___field2}';

... and calc1 needs to do "{mytable___calc2} * (int)'{mytable___field3}'", calc1 would have to do ...

$calc2val = (int)'{mytable___field1}' + (int)'{mytable___field2}';
return $calc2val * (int)'{mytable___field3}';

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

Thank you.

Members online

No members online now.
Back
Top