3

For an event page I have the option to filter on cities and countries. Each event has entry fields in which the city and country, of which the values are outputted in a drop down list on the front end.

As some events happen in the same country and city I have multipe values that are the same. Is there any chance to filter these values in Craft and output it only ones?

{% for entry in craft.entries.section('events') %}                      
    <option value="{{entry.stad}}" name="{{ entry.stad }}">{{ entry.stad }}</option>
{% endfor %}

2 Answers 2

3

Craft has a group filter, that does just that:

{% for stad, events in craft.entries.section('events') | group('stad') %}
    <option value="{{ stad }}">{{ stad }}</option>
{% endfor %}
0
2

As you are outputting the options, build an array of stad values and check for duplicates on each loop iteration. If the value does not exist in the array, add it to the array and print out the option.

{% set stad_array = [] %}

{% for entry in craft.entries.section('events') %}
    {% if entry.stad not in stad_array %}
        {% set stad_array = stad_array|merge([entry.stad]) %}
        <option value="{{entry.stad}}" name="{{ entry.stad }}">{{ entry.stad }}</option>
    {% endif %}
{% endfor %}

Hope this helps!

2
  • Thanks for the reply :) However, the other option is easier Mar 1, 2015 at 11:11
  • Agreed! Glad you found a solution :) Mar 1, 2015 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.