# Sum up number fields in the loop

I don't understand what's going on in my code logic because I'm getting unexpected output.

I'd like to sum up three number fields in entries loop for the given post date range. Output gets almost doubled as expected. What am I missing here?

``````{# Get this month's data #}
{% set thisMonthStatistic = orders.postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{% if thisMonthStatistic|length %}
{# We must set variable "0" and pass it in for the looping's variable #}
{% set thisMonthEarning = 0 %}

{# looping in "thisMonthStatistic" #}
{% for statistic in thisMonthStatistic %}

{% set aPrice = statistic.aPrice %}
{% set bPrice = statistic.bPrice %}
{% set cPrice = statistic.cPrice %}

{% if aPrice > 0 %}
{% set aPrice = aPrice + thisMonthEarning %}
{% endif %}

{% if bPrice > 0 %}
{% set bPrice = bPrice + thisMonthEarning %}
{% endif %}

{% if cPrice > 0 %}
{% set cPrice = cPrice + thisMonthEarning %}
{% endif %}

{% set thisMonthEarning = aPrice + bPrice + cPrice %}

{% endfor %}

{% else %}

{% set thisMonthEarning = 0 %}

{% endif %}

{# Final variable output #}
{{ thisMonthEarning }}
``````

Try this:

``````{# Get this month's data #}
{% set thisMonthStatistic = orders.postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{# We must set variable "0" and pass it in for the looping's variable #}
{% set thisMonthEarning = 0 %}

{# looping in "thisMonthStatistic". 0 times if none exists #}
{% for statistic in thisMonthStatistic %}

{# fetch aPrice, bPrice, cPrice, but only if > 0 #}
{% set aPrice = (statistic.aPrice > 0) ? statistic.aPrice : 0 %}
{% set bPrice = (statistic.bPrice > 0) ? statistic.bPrice : 0 %}
{% set cPrice = (statistic.cPrice > 0) ? statistic.cPrice : 0 %}

{% set thisMonthEarning = thisMonthEarning + aPrice + bPrice + cPrice %}

{% endfor %}

{# Final variable output #}
{{ thisMonthEarning }}
``````

You don't need the `{% if thisMonthStatistic|length %}` - if there is no data, you will go through the loop 0 times, and `thisMonthEarning` will be `0`.

I am not sure what you intend to do if `aPrice` is less than 0. My guess is that you want to just skip those, which is what the `?:` operator is doing here.

Another option (this one includes the prices < 0):

``````{% set thisMonthStatistic = orders.postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{% set aPrice = 0 %}
{% set bPrice = 0 %}
{% set cPrice = 0 %}

{% for statistic in thisMonthStatistic %}

{% set aPrice = aPrice + statistic.aPrice %}
{% set bPrice = bPrice + statistic.bPrice %}
{% set cPrice = cPrice + statistic.cPrice %}

{% endfor %}

{{ aPrice + bPrice + cPrice }}
``````
• Simple and short. First option win for my needs. Great @Marion Newlevant :) – Dominik Krulak Sep 7 '16 at 19:39

Hmm... hard to tell because I canâ€™t know what your expected result would be. But if I had to guess then you might be looking for just this:

``````{% if aPrice %}
{% set thisMonthEarning = thisMonthEarning + aPrice %}
{% endif %}
``````
• Hi Carl! Isn't that basically what I have? I need to sum up three different variables at the end. I get correct output If I only set one variable let's say for 'aPrice'. When I add the other one in the way like It's in the question It gets wrong. – Dominik Krulak Sep 7 '16 at 16:19
• Yeah, because you add the initial `thisMonthEarning` with every addition again. – carlcs Sep 7 '16 at 16:30
• Try what I suggested for each price and get rid of your `{% set thisMonthEarning = aPrice + bPrice + cPrice %}` thingy at the end. – carlcs Sep 7 '16 at 16:30
• O yeah, 1st `thisMonthEarning` is passed over to 2nd and (1st + 2nd) over to 3rd `thisMonthEarning` in the loop. That way all three variables get summed up correctly. I like it if I'm right. I vote it up definitely, but @Marion Newlevant 's answer is very close to the one I had at first place and it's shorter in code eventually. From performance perspective I didn't find any down side from both answers. It queries the same count of queries from database for both answers too. So saving document size. Thanks @carlcs again :) – Dominik Krulak Sep 7 '16 at 19:36