<p>What you could try is putting the "if statement" as a condition in the for loop like this:</p>

<pre><code>{% for premiereItem in entry.kompositonsart if premiereItem.value == 'ua' %} //the checkbox loop starts
    // only return checked value with 'ua'
    {{ countPremiere }}
{% endfor %}
</code></pre>

<p>I'm not really sure what you are asking but if my thoughts are right this should do the trick. This way it wont effect any other regular loop functions like a <code>loop.index</code> if you are going to use it.</p>

CC BY-SA 3.0
2017-06-12T11:55:01.477Z
2017-06-12T11:55:01.477Z
Justin Dekkers
<p>I have a checkbox field which I want the sum of a certain checkbox:</p>

<pre><code>{% set countPremiere = 1 %}
{% set iterationPremiere = 0 %}
{% set premiereTotal = 0 %}

{% for entry in allEntries %} // the normal loop starts

  {% for premiereItem in entry.kompositonsart %} //the checkbox loop starts
    {%- if premiereItem.value == 'ua' -%} // only return checked value with 'ua'
      {{ countPremiere }}
    {% endif %}
  {%- endfor -%}

  {% set premiereTotal = iterationPremiere +  premiereTotal %}

{% endfor %}

{{ premiereTotal }}
</code></pre>

<p>That's basically the same as I did <a href="https://craftcms.stackexchange.com/questions/16412/total-sum-within-loop">here</a>. This doesn't work however. I suspect it is not working because the way checkboxes are looped. Any ideas?</p>

<templating><checkboxes><math>
Sum of one checkbox field
# Sum of one checkbox field

(Asked by: username_0 on 2017-06-12)

I have a checkbox field which I want the sum of a certain checkbox:
{% set countPremiere = 1 %}
{% set iterationPremiere = 0 %}
{% set premiereTotal = 0 %}

{% for entry in allEntries %} // the normal loop starts

  {% for premiereItem in entry.kompositonsart %} //the checkbox loop starts
    {%- if premiereItem.value == 'ua' -%} // only return checked value with 'ua'
      {{ countPremiere }}
    {% endif %}
  {%- endfor -%}

  {% set premiereTotal = iterationPremiere +  premiereTotal %}

{% endfor %}

{{ premiereTotal }}

That's basically the same as I did here. This doesn't work however. I suspect it is not working because the way checkboxes are looped. Any ideas?

**Tags:** templating, checkboxes, math

---

## 1 answers

### Answer by username_1 on 2017-06-12. Score: 2 | Accepted answer

What you could try is putting the "if statement" as a condition in the for loop like this:
{% for premiereItem in entry.kompositonsart if premiereItem.value == 'ua' %} //the checkbox loop starts
    // only return checked value with 'ua'
    {{ countPremiere }}
{% endfor %}

I'm not really sure what you are asking but if my thoughts are right this should do the trick. This way it wont effect any other regular loop functions like a loop.index if you are going to use it.


---

