XF 2.0 Sum of numbers in a foreach loop

Case

Well-known member
The following returns: 5 4

PHP:
<xf:foreach loop="$resources" value="$resource">
    {{$resource.rating_avg}}
</xf:foreach>

I'm trying to return the sum, so 9.

I thought I could do something like...

PHP:
<xf:set var="$total">0</xf:set>
<xf:foreach loop="$resources" value="$resource">
    <xf:set var="$total">{{$total+$resource.rating_avg}}</xf:set>
</xf:foreach>
{{ $total }}
 
I would expect it not to work, but that output is confusing as I'm not certain I'd expect that, exactly.

But this would probably work:
HTML:
<xf:set var="$total">0</xf:set>
<xf:foreach loop="$resources" value="$resource">
    <xf:set var="$total" value="{{ $total + $resource.rating_avg }}" />
</xf:foreach>
{{ $total }}
 
I would expect it not to work, but that output is confusing as I'm not certain I'd expect that, exactly.

But this would probably work:
HTML:
<xf:set var="$total">0</xf:set>
<xf:foreach loop="$resources" value="$resource">
    <xf:set var="$total" value="{{ $total + $resource.rating_avg }}" />
</xf:foreach>
{{ $total }}
That kinda works. It adds up to 10 but I can probably work with that. Cheers
 
That's kind of odd, but I assume you're dealing with values such as 5.4 + 4.5 or similar so 9.9 rounded up as an integer is 10. To get around that, your total likely needs to be filtered and formatted as a number:
Code:
{{ $total|number(1) }}
The (1) in brackets is the precision i.e. how many numbers after the decimal.
 
That's kind of odd, but I assume you're dealing with values such as 5.4 + 4.5 or similar so 9.9 rounded up as an integer is 10. To get around that, your total likely needs to be filtered and formatted as a number:
Code:
{{ $total|number(1) }}
The (1) in brackets is the precision i.e. how many numbers after the decimal.
Ah, that works but I get a template error at the top of the page.

Template public:xfrm_category_view: number_format() expects parameter 1 to be float, object given (src/XF/Language.php:804)
 
That would be odd if you're still using the changed code I recommended. I'd expect that from the original code you were using.
 
That would be odd if you're still using the changed code I recommended. I'd expect that from the original code you were using.
This is the code I'm using...
PHP:
<xf:set var="$total">0</xf:set>
<xf:foreach loop="$resources" value="$resource">
    <xf:set var="$total" value="{{ $total + $resource.rating_avg }}" />
</xf:foreach>
{{ $total|number(1) }}
Which is still actually totalling 1 too many. Plus the template error.
 
Oh it’s the first line.

That needs to have a similar change (the tag needs to be self closing and you need to move the 0 value to the value attribute)
 
Oh it’s the first line.

That needs to have a similar change (the tag needs to be self closing and you need to move the 0 value to the value attribute)
OK, so this?

PHP:
<xf:set var="$total" value="0" />
<xf:foreach loop="$resources" value="$resource">
    <xf:set var="$total" value="{{ $total + $resource.rating_avg }}" />
</xf:foreach>
{{ $total|number(1) }}

That gives the correct sum but the template error remains.
 
I'm using that exact code at the bottom of my xfrm_category_view template and there's no template error.

Not sure if the error has changed? The only reason an object would be involved was this line which you have now changed:
Code:
<xf:set var="$total">0</xf:set>
It happens because the above actually creates an XF\PreEscaped object. With the approach you should be using now it sets the raw value directly. The only other possible change to make is:

Code:
<xf:set var="$total" value="{{ 0 }}" />

Which would initialise the value as an integer, rather than a string, but honestly, it shouldn't be required and works fine for me on all PHP versions from PHP 5.6 up to 7.2.
 
I'm using that exact code at the bottom of my xfrm_category_view template and there's no template error.

Not sure if the error has changed? The only reason an object would be involved was this line which you have now changed:
Code:
<xf:set var="$total">0</xf:set>
It happens because the above actually creates an XF\PreEscaped object. With the approach you should be using now it sets the raw value directly. The only other possible change to make is:

Code:
<xf:set var="$total" value="{{ 0 }}" />

Which would initialise the value as an integer, rather than a string, but honestly, it shouldn't be required and works fine for me on all PHP versions from PHP 5.6 up to 7.2.
Agh! Fixed it. I commented out my old code only using <!-- --> and not xf:comment so it was still checking that.

Cheers!
 
Back
Top Bottom