Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-12T11:55:01.477Z
Answers.LastActivityDate: 2017-06-12T11:55:01.477Z
Answers.UserDisplayName: Justin Dekkers
Body: <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>
  
Tags: <templating><checkboxes><math>
Title: Sum of one checkbox field
ThreadText: # 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](https://craftcms.stackexchange.com/questions/16412/total-sum-within-loop). 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.
  
  
  ---
  
  