<p>You have three <code>for</code> loops, and are setting <code>currentMonthEarning</code> to <code>0</code> outside of all of them, and then adding to it (and never resetting it to <code>0</code>).</p>

<p>Probably you should move the <code>{% set currentMonthEarning = 0 %}</code> inside either the first or second <code>for</code> loop.</p>

CC BY-SA 3.0
2016-09-08T19:18:15.537Z
2016-09-08T19:18:15.537Z
Marion Newlevant
<p>I don't even know how I should ask the question. I'll try to explain it as clear as I can.</p>

<p>I want to order entries by year-month and output single value <em>(from certain number fields)</em> from all entries from a month like so: </p>

<ol>
<li><em>( July(10) )</em></li>
<li><em>( August(20) )</em></li>
<li><em>( September(30) )</em></li>
</ol>

<p>Here is the piece of code I'm trying to figure out:</p>

<pre><code>[
{% set allEntries = craft.entries.section('orders').limit(null).order('postDate desc') %}

{% set currentMonthEarning = 0 %}

{% for year, entriesInYear in allEntries | group("postDate.year") %}
    {% for month, entries in entriesInYear | group("postDate|date('F')" ) %}
        {% for entry in entries %}

            {# fetch aPrice, bPrice, cPrice, but only if &gt; 0 #}
            {% set aPrice = (entry.aPrice &gt; 0) ? entry.aPrice : 0 %}
            {% set bPrice = (entry.bPrice &gt; 0) ? entry.bPrice : 0 %}
            {% set cPrice = (entry.cPrice &gt; 0) ? entry.cPrice : 0 %}

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

        {% endfor %}

        {{ currentMonthEarning }}{{ not loop.last ? ', ' }}

    {% endfor %}
{% endfor %}
]
</code></pre>

<p><strong>Desire output would be:</strong> <code>[10,20,30]</code></p>

<p><strong>What's going on there actually?:</strong> It loops through current year's entries from section <code>mySection</code>, then months and then entries. While it loops through <code>entries</code>, it does summing up of three number fields and sets it into the variable <code>currentMonthEarning</code> outside the <code>entries</code> loop.</p>

<p><strong>Current output:</strong> <code>[10,30,70]</code></p>

<p>It seems that <code>currentMonthEarning</code> is passed over to the next "month" with a value set from previous "month" in the loop. The value for the last month (latest) is correct because of <code>('postDate desc')</code>. If you understand what I'd like can you find a way to get it done?</p>

CC BY-SA 3.0
2016-09-08T17:15:21.433Z
Not sure I'm fully understanding, but are you looking to just reset `currentMonthEarning` after outputting its value?  i.e. `{{ currentMonthEarning }}{{ not loop.last ? ', ' }}{% set currentMonthEarning = 0 %}`?
Brad Bell
<variable>
Reseting variable
# Reseting variable

(Asked by: username_0 on 2016-09-08)

I don't even know how I should ask the question. I'll try to explain it as clear as I can.
I want to order entries by year-month and output single value (from certain number fields) from all entries from a month like so: 

( July(10) )
( August(20) )
( September(30) )

Here is the piece of code I'm trying to figure out:
[
{% set allEntries = craft.entries.section('orders').limit(null).order('postDate desc') %}

{% set currentMonthEarning = 0 %}

{% for year, entriesInYear in allEntries | group("postDate.year") %}
    {% for month, entries in entriesInYear | group("postDate|date('F')" ) %}
        {% for entry in entries %}

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

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

        {% endfor %}

        {{ currentMonthEarning }}{{ not loop.last ? ', ' }}

    {% endfor %}
{% endfor %}
]

Desire output would be: [10,20,30]
What's going on there actually?: It loops through current year's entries from section mySection, then months and then entries. While it loops through entries, it does summing up of three number fields and sets it into the variable currentMonthEarning outside the entries loop.
Current output: [10,30,70]
It seems that currentMonthEarning is passed over to the next "month" with a value set from previous "month" in the loop. The value for the last month (latest) is correct because of ('postDate desc'). If you understand what I'd like can you find a way to get it done?

**Tags:** variable
> **Comments:**
> **username_1**: Not sure I'm fully understanding, but are you looking to just reset `currentMonthEarning` after outputting its value?  i.e. `{{ currentMonthEarning }}{{ not loop.last ? ', ' }}{% set currentMonthEarning = 0 %}`?  

---

## 1 answers

### Answer by username_2 on 2016-09-08. Score: 2 | Accepted answer

You have three `for` loops, and are setting `currentMonthEarning` to `0` outside of all of them, and then adding to it (and never resetting it to `0`).

Probably you should move the `{% set currentMonthEarning = 0 %}` inside either the first or second `for` loop.


---

