Calc Element to sum repeatable rows acts odd with only 1 row

kdiamond

Member
I have a calc element that calulates the total from repeatable rows. Very odd thing is when there is only ONE row, it takes the amount entered on that row and it does a divide by 4. Example, if the row is 12, the calc total is 3. If the row is 24, the total displayed is 6. It works fine when there are >1 rows.

Here is the calc:

$sold = FabrikWorker::JSONtoData($data['pet_event_report_products___format_sold_raw'], true);
$total= implode(',', $sold);
return array_sum(str_split($total));


On further testing it's not always /4. I'm not sure what it is. Some small number are not affected.
 

Attachments

  • Capture.JPG
    Capture.JPG
    65.8 KB · Views: 30
Last edited:
Not sure why you are doing an implode() and then a str_split(). The return from JSONtoData() for a repeat element should already be an array of values. And str_split() will split on individual characters (unless you give it a length to split on), so the number (say) 24 will get split into an array of [2,4], so yes, the sum is 6.

You should just be able to do ...

Code:
$sold = FabrikWorker::JSONtoData($data['pet_event_report_products___format_sold_raw'], true);
return array_sum($sold);

-- hugh
 
That got it - thank you. I think I did that because in testing the result was coming back and a comma string, 2,5,7 as example. Clearly it wasn't needed.
 
I think it would only be comma separated if you used a placeholder.

And just for future reference, even if it was comma separated, you wouldn't need the implode or str_split. Just explode() the string into an array, then array_sum() that.

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

Thank you.

Members online

Back
Top