<p>Something like this should work:</p>

<pre><code>{% set reviews = craft.entries.section('reviews').relatedto(entry) %}
{% set value = 0 %}
{% set condition = 0 %}
{% set reception = 0 %}
{% set count = 0 %}
{% for review in reviews %}
    {% set value = value + review.value %}
    {% set condition = condition + review.condition %}
    {% set reception = reception + review.reception %}
    {% set count = loop.index %}
{% endfor %}
{% set averageValue = (value / count)|round(1, 'floor') %}
{% set averageCondition = (condition / count)|round(1, 'floor') %}
{% set averageReception = (reception / count)|round(1, 'floor') %}
</code></pre>

CC BY-SA 3.0
2015-04-12T10:29:06.133Z
Hey Douglas, much appreciated, that works like a charm!
patrick C
CC BY-SA 3.0
2015-04-12T06:54:00.393Z
2015-04-12T07:01:46.490Z
2015-04-12T07:01:46.490Z
Douglas McDonald
<p>I've setup a section for reviews with several fields as numbers for a rating 0-5, I'm trying to figure out how to get the total of all reviews and sum of all reviews to provide a single average for an entry the reviews relate to.</p>

<p>What's the best approach to this.</p>

<p>Example of my fields:</p>

<p>Value
Condition
Reception
etc</p>

<p>all graded 0-5 (number fields).</p>

<p>So how to go through the foreach and get the sum of all fields from all reviews then divide by total reviews for the related entry to get the average for each field.</p>

CC BY-SA 3.0
2015-04-11T21:54:16.650Z
Are you looking to get the average for a single entry or an overall average across every entry in you reviews section?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-12T10:30:46.773Z
Hi Aaron, Douglas' code sample works, that will give me the average across each facet of a review, for the overall I will be able to define my own formula with weighting applied to each in order to calculate the total score out of 100 (as some facets have a lower importance to the overall score).
patrick C
CC BY-SA 3.0
2015-04-12T14:09:54.800Z
Great! Be sure to mark his answer as correct!
Aaron Berkowitz
<forloop>
calculating average ratings from a series of review fields
# calculating average ratings from a series of review fields

(Asked by: username_0 on 2015-04-11)

I've setup a section for reviews with several fields as numbers for a rating 0-5, I'm trying to figure out how to get the total of all reviews and sum of all reviews to provide a single average for an entry the reviews relate to.

What's the best approach to this.

Example of my fields:

Value
Condition
Reception
etc

all graded 0-5 (number fields).

So how to go through the foreach and get the sum of all fields from all reviews then divide by total reviews for the related entry to get the average for each field.

**Tags:** forloop

> **Comments:**
> **username_1**: Are you looking to get the average for a single entry or an overall average across every entry in you reviews section?  
> **username_0**: Hi Aaron, Douglas' code sample works, that will give me the average across each facet of a review, for the overall I will be able to define my own formula with weighting applied to each in order to calculate the total score out of 100 (as some facets have a lower importance to the overall score).  
> **username_1**: Great! Be sure to mark his answer as correct!  

---

## 1 answers

### Answer by username_2 on 2015-04-12. Score: 5

Something like this should work:
{% set reviews = craft.entries.section('reviews').relatedto(entry) %}
{% set value = 0 %}
{% set condition = 0 %}
{% set reception = 0 %}
{% set count = 0 %}
{% for review in reviews %}
    {% set value = value + review.value %}
    {% set condition = condition + review.condition %}
    {% set reception = reception + review.reception %}
    {% set count = loop.index %}
{% endfor %}
{% set averageValue = (value / count)|round(1, 'floor') %}
{% set averageCondition = (condition / count)|round(1, 'floor') %}
{% set averageReception = (reception / count)|round(1, 'floor') %}

> **Comments:**
> **username_0**: Hey Douglas, much appreciated, that works like a charm!  

---

