Sum of a repeated group in the calc plugin

wouter.galia

New Member
Hi

I use the calc plug-in to calculate the sum of a repeated group with the code underneath (found in forum).
My setup is very much like the 'venues' and 'bands' tutorial.

Everything works fine except for when I have one item only. With only one item the result is '0'. With more then one item it works fine.

I also would like to put the '?' sign in front.

-----------------------

$fields = array('tablename___listname');
$total = 0;
foreach ($fields as $field)
{
$value = JArrayHelper::getValue($data, $field . '_raw', array());
$thisSum = array_sum($value);
$total += $thisSum;
}
return $total;

-----------------------

Best regards
 
Try just ...

Code:
$amounts = (array) $data['tablename___element'];
return '?' . array_sum($amounts);

-- hugh
 
This does the opposite. It only works with one item. With more then one item in the repeating group it returns '0'.

The ? sign is not needed anymore, it gives a conflict with fallowing calculations.
 
Use
$amounts = (array) $data['tablename___element_raw'];
fallowing calculations
You can't use a calc element in other calcs because you can't know which is calculated first.
 
Oops, yes, I accidentally left off the raw when I replaced my actual element name after testing that code.

Note that this won't work as expected if you calc on list display, with unmarked rows.

So set calc on save to yes.


Sent from my HTC One using Tapatalk
 
Perfect! That did it.

$amounts = (array) $data['tablename___element_raw''];
return array_sum($amounts);

Then when I use the result of this calc element in another calc element for calculating a discount, everything works fine.

Many thanks!
 
Last edited:
It's working on my site.
Make sure to have 3 underscores in your element name and set Joomla error reporting to max to see warnings and notices.
Do you have ajax calculation off or on?
Can you post a screenshot of your settings?
 
Oh yes, I should have done a reload before submitting the answer (opened it before lunch;))
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top