Answers.Body: <p>Twig allows you to add variables just as they were integers. You'll want to setup your <code>totalHours</code> variable before your loop begins so that the <code>totalHours</code> variable is available in the scope outside of the loop itself.</p>
  
  <p>Something like this should work for you:</p>
  
  <pre><code>{# Create a variable to track your total hours #}
  {% set totalHours = 0 %}
  
  {% for block in entry.matrixFieldHandle %}
  
      {% if block.type == 'hoursBlock' %}
          {# Increment your total hours by adding the value
             of your hours block to the current totalHours #}
          {% set totalHours = totalHours + block.hoursFieldHandle %}
      {% endif %}
  
  {% endfor %}
  
  {# Output the result #}
  Total Hours: {{ totalHours }}
  </code></pre>
  
  <p>While I can't test your specific code, here's a generic example that is doing the same thing that will work if you copy and paste it to your template:</p>
  
  <pre><code>{% set totalHours = 0 %}
  
  {% for hours in 0..3 %}
      {% set totalHours = totalHours + hours %}
  {% endfor %}
  
  Total Hours: {{ totalHours }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-20T14:40:43.530Z
Answers.Text: Perfect - It never occurred to me to set the variable initially blank and then iterate over it. Still new to this craft thing :) Thanks!
Answers.UserDisplayName: FortySeven Media
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-20T04:46:05.383Z
Answers.LastActivityDate: 2016-08-20T15:10:34.157Z
Answers.LastEditDate: 2016-08-20T15:10:34.157Z
Answers.UserDisplayName: Ben Parizek
Body: <p>I have a Matrix block with a description field and a number field for hours. Let's say while creating an entry I add in 3 of these blocks with each having a different amount for the hours.</p>
  
  <p>How can I add each of those 3 amounts and get a total amount?</p>
  
  <p>I've spent quite a while researching and can't figure out what kind of syntax would make this work.</p>
  
Tags: <templating><matrix><math>
Title: Adding all the Matrix Block field types of the same kind to get a total
ThreadText: # Adding all the Matrix Block field types of the same kind to get a total
  
  (Asked by: username_0 on 2016-08-20)
  
  I have a Matrix block with a description field and a number field for hours. Let's say while creating an entry I add in 3 of these blocks with each having a different amount for the hours.
  
  How can I add each of those 3 amounts and get a total amount?
  
  I've spent quite a while researching and can't figure out what kind of syntax would make this work.
  
  **Tags:** templating, matrix, math
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-20. Score: 4 | Accepted answer
  
  Twig allows you to add variables just as they were integers. You'll want to setup your `totalHours` variable before your loop begins so that the `totalHours` variable is available in the scope outside of the loop itself.
  
  Something like this should work for you:
  
  ```
  {# Create a variable to track your total hours #}
  {% set totalHours = 0 %}
  
  {% for block in entry.matrixFieldHandle %}
  
      {% if block.type == 'hoursBlock' %}
          {# Increment your total hours by adding the value
             of your hours block to the current totalHours #}
          {% set totalHours = totalHours + block.hoursFieldHandle %}
      {% endif %}
  
  {% endfor %}
  
  {# Output the result #}
  Total Hours: {{ totalHours }}
  
  ```
  
  While I can't test your specific code, here's a generic example that is doing the same thing that will work if you copy and paste it to your template:
  
  ```
  {% set totalHours = 0 %}
  
  {% for hours in 0..3 %}
      {% set totalHours = totalHours + hours %}
  {% endfor %}
  
  Total Hours: {{ totalHours }}
  
  ```
  
  > **Comments:**
  > **username_0**: Perfect - It never occurred to me to set the variable initially blank and then iterate over it. Still new to this craft thing :) Thanks!  
  
  ---
  
  