<p>Try something like this:</p>

<pre><code>&lt;form class="event-filter"&gt;
    &lt;label for="filter"&gt;Filter event: &lt;/label&gt;
    &lt;select id="filter" name="filter"&gt;
        {% for event in craft.entries.section('event') %}    
            {% set numEventBookings = craft.entries.section('booking').relatedTo(event).total() %}
            &lt;option value="i{{event.id}}"&gt;{{event.title}} ({{numEventBookings}} bookings)&lt;/option&gt;
        {% endfor %}
    &lt;/select&gt;
&lt;/form&gt;
</code></pre>

<p>But it'll generate one database query pr event, so it might be a bit inefficient, depending on your setup.</p>

CC BY-SA 3.0
2014-08-30T17:13:20.607Z
This works, thank you :) That page is already pretty heft for database queries I think.. It's already querying the database for every booking multiple times... I'm not good at templating optimisation! If anyone has any ideas on optimising it further, comments welcome. Thanks aelvan.
darylknight
CC BY-SA 3.0
2014-08-30T19:59:46.133Z
If you already are getting all the bookings and have them in the DOM of the page, I'd consider just doing the counting in the frontend with javascript/jquery. Just make a reference on each booking entry to the event, do some query'ing and insert the count in the appropriate places. Client side work is cheap. :)
André Elvan
CC BY-SA 3.0
2014-08-30T15:32:54.167Z
2014-08-30T15:32:54.167Z
André Elvan
<p>I have a template which displays a table of all the bookings on the website. The booking channel entries consist of two fields: a related user, and a related event, 1:1. There's a drop-down list of all the events at the top of the template which lists all of the events on the site. When a user clicks on an event title, the table updates to only show bookings related to that event (done by applying a class to all rows, and hiding every row which doesn't have the same class as the chosen event slug). The filter code looks like this:</p>

<pre><code>&lt;form class="event-filter"&gt;
    &lt;label for="filter"&gt;Filter event: &lt;/label&gt;
    &lt;select id="filter" name="filter"&gt;
        {% for event in craft.entries.section('event') %}
        &lt;option value="i{{event.id}}"&gt;{{event.title}}&lt;/option&gt;
        {% endfor %}
    &lt;/select&gt;
&lt;/form&gt;
</code></pre>

<p>The bookings table displays all the events using something like this:</p>

<pre><code>{% set bookings = craft.entries.section('booking').order('postDate desc') %}
</code></pre>

<p>Is there a way for my filter form to list my events, and display the number of bookings for each event? So it would say, for example:</p>

<ul>
<li>First event (3 bookings)</li>
<li>Second event (8 bookings)</li>
</ul>

<p>I'm sure it must be possible using relations, but I can't work out the syntax.</p>

<entries><relations>
How can I display the number of related entries?
# How can I display the number of related entries?

(Asked by: username_0 on 2014-08-30)

I have a template which displays a table of all the bookings on the website. The booking channel entries consist of two fields: a related user, and a related event, 1:1. There's a drop-down list of all the events at the top of the template which lists all of the events on the site. When a user clicks on an event title, the table updates to only show bookings related to that event (done by applying a class to all rows, and hiding every row which doesn't have the same class as the chosen event slug). The filter code looks like this:

    Filter event: 
    
        {% for event in craft.entries.section('event') %}
        {{event.title}}
        {% endfor %}
    


The bookings table displays all the events using something like this:
{% set bookings = craft.entries.section('booking').order('postDate desc') %}

Is there a way for my filter form to list my events, and display the number of bookings for each event? So it would say, for example:

First event (3 bookings)
Second event (8 bookings)

I'm sure it must be possible using relations, but I can't work out the syntax.

**Tags:** entries, relations

---

## 1 answers

### Answer by username_1 on 2014-08-30. Score: 4 | Accepted answer

Try something like this:

    Filter event: 
    
        {% for event in craft.entries.section('event') %}    
            {% set numEventBookings = craft.entries.section('booking').relatedTo(event).total() %}
            {{event.title}} ({{numEventBookings}} bookings)
        {% endfor %}
    


But it'll generate one database query pr event, so it might be a bit inefficient, depending on your setup.

> **Comments:**
> **username_0**: This works, thank you :) That page is already pretty heft for database queries I think.. It's already querying the database for every booking multiple times... I'm not good at templating optimisation! If anyone has any ideas on optimising it further, comments welcome. Thanks aelvan.  
> **username_1**: If you already are getting all the bookings and have them in the DOM of the page, I'd consider just doing the counting in the frontend with javascript/jquery. Just make a reference on each booking entry to the event, do some query'ing and insert the count in the appropriate places. Client side work is cheap. :)  

---

