<p>If I understand things correctly, you have a matrix block <code>telephone</code>, which contains a dropdown field <code>telephone.icon</code>, and you want the value of the selected dropdown option. That value is <code>telephone.icon.value</code> (which will be one of <em>mobile</em>, <em>home</em>, <em>work</em>, or <em>other</em>).</p>

<pre><code>{% for telephone in user.profilePhone %}
  &lt;li&gt;
    {% if telephone.icon.value == 'mobile' %}
      &lt;i class="fa fa-fw fa-mobile"&gt;&lt;/i&gt;
    {% elseif telephone.icon.value == 'home' %}
      &lt;i class="fa fa-fw fa-home"&gt;&lt;/i&gt;
    {% else %}
      &lt;i class="fa fa-fw fa-phone"&gt;&lt;/i&gt;
    {% endif %}

    {% if telephone.country %}{{ telephone.country }}{% endif %}
    {{ telephone.number }}
  &lt;/li&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-07-28T17:04:35.140Z
Ah, seems to work great! I didn't realise you could access this outside of the `{% for option in telephone.icon.options %}` loop. So, presumably you could use a similar method for radio buttons etc — are there any snippets of this in the docs? It's not clear from the [dropdown snippets](http://buildwithcraft.com/docs/dropdown-fields) yet it's pretty simple to do. Thanks!
Rob
CC BY-SA 3.0
2015-07-27T06:48:16.687Z
2015-07-27T06:48:16.687Z
Marion Newlevant
<p>Ok, so I've tidied up the code a bit and using <code>option.selected</code> instead of <code>option</code> to see if the option is selected. But, it isn't behaving how I'd expect it:</p>

<pre><code>{% for telephone in user.profilePhone.type('details') %}
    &lt;li&gt;
      {% if telephone.type == 'details' %}
        {% for option in telephone.icon.options %}
            {% if option.selected == 'mobile' %}
                &lt;i class="fa fa-fw fa-mobile"&gt;&lt;/i&gt;
            {% elseif option.selected == 'home' %}
                &lt;i class="fa fa-fw fa-home"&gt;&lt;/i&gt;
            {% else %}
                &lt;i class="fa fa-fw fa-phone"&gt;&lt;/i&gt;
            {% endif %}
        {% endfor %}
        {% if telephone.country %}{{ telephone.country }}{% endif %}
        {{ telephone.number }}
      {% endif %}
    &lt;/li&gt;
{% endfor %}
</code></pre>

<p>So this is how I read it:</p>

<ol>
<li>Loop through all Block Types <code>details</code>

<ul>
<li>As I understood from the docs, there is no need to be specific if there's only one Block Type (regardless of max blocks setting). The extra <code>if</code> conditional seems redundant?</li>
<li>I also understood that a <code>for loop</code> would act as a conditional anyway, so it would return false if no Blocks existed?</li>
</ul></li>
<li>Loop through the <code>icon</code> dropdown options

<ul>
<li>Whichever one is selected, display it's corresponding icon.</li>
</ul></li>
</ol>

<p>But what it actually outputs is this:</p>

<p><a href="https://i.sstatic.net/EGvf6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/EGvf6.png" alt="Wrong icon output"></a></p>

<p>So it's outputting all of the available options, not just the selected one (and not even the correct icons). I've managed (with separate if statements for just <code>'home'</code> and <code>'mobile'</code> to get this:</p>

<p><a href="https://i.sstatic.net/zEZaX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zEZaX.png" alt="enter image description here"></a></p>

<p>But it's still outputting all options declared in the if statement, instead of the desired single icon. Here are the dropdown options:</p>

<p><a href="https://i.sstatic.net/MzO6o.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/MzO6o.png" alt="enter image description here"></a></p>

<p>I'm new to programming, so perhaps I'm getting muddled up with things, but it seems like it should only output the icon I want, not the entire dropdown list (I realise I may need another conditional for if <code>'other'</code> or <code>'work'</code> is selected)</p>

<p>Perhaps the issue is you can't check in the <code>for loop</code> for <strong>just</strong> the selected option with an <a href="http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition" rel="nofollow noreferrer">if condition</a>. Something like:</p>

<p><code>{% for option in telephone.icon.options if option.selected %}</code></p>

<matrix><field><conditional><fieldtypes><drop-down>
Conditional if statement for Matrix dropdown option
# Conditional if statement for Matrix dropdown option

(Asked by: username_0 on 2015-07-24)

Ok, so I've tidied up the code a bit and using option.selected instead of option to see if the option is selected. But, it isn't behaving how I'd expect it:
{% for telephone in user.profilePhone.type('details') %}
    
      {% if telephone.type == 'details' %}
        {% for option in telephone.icon.options %}
            {% if option.selected == 'mobile' %}
                
            {% elseif option.selected == 'home' %}
                
            {% else %}
                
            {% endif %}
        {% endfor %}
        {% if telephone.country %}{{ telephone.country }}{% endif %}
        {{ telephone.number }}
      {% endif %}
    
{% endfor %}

So this is how I read it:

Loop through all Block Types details

As I understood from the docs, there is no need to be specific if there's only one Block Type (regardless of max blocks setting). The extra if conditional seems redundant?
I also understood that a for loop would act as a conditional anyway, so it would return false if no Blocks existed?

Loop through the icon dropdown options


Whichever one is selected, display it's corresponding icon.


But what it actually outputs is this:

So it's outputting all of the available options, not just the selected one (and not even the correct icons). I've managed (with separate if statements for just 'home' and 'mobile' to get this:

But it's still outputting all options declared in the if statement, instead of the desired single icon. Here are the dropdown options:

I'm new to programming, so perhaps I'm getting muddled up with things, but it seems like it should only output the icon I want, not the entire dropdown list (I realise I may need another conditional for if 'other' or 'work' is selected)
Perhaps the issue is you can't check in the for loop for just the selected option with an if condition. Something like:
{% for option in telephone.icon.options if option.selected %}

**Tags:** matrix, field, conditional, fieldtypes, drop-down


---

## 1 answers

### Answer by username_1 on 2015-07-27. Score: 1 | Accepted answer

If I understand things correctly, you have a matrix block telephone, which contains a dropdown field telephone.icon, and you want the value of the selected dropdown option. That value is telephone.icon.value (which will be one of mobile, home, work, or other).
{% for telephone in user.profilePhone %}
  
    {% if telephone.icon.value == 'mobile' %}
      
    {% elseif telephone.icon.value == 'home' %}
      
    {% else %}
      
    {% endif %}

    {% if telephone.country %}{{ telephone.country }}{% endif %}
    {{ telephone.number }}
  
{% endfor %}

> **Comments:**
> **username_0**: Ah, seems to work great! I didn't realise you could access this outside of the `{% for option in telephone.icon.options %}` loop. So, presumably you could use a similar method for radio buttons etc — are there any snippets of this in the docs? It's not clear from the [dropdown snippets](http://buildwithcraft.com/docs/dropdown-fields) yet it's pretty simple to do. Thanks!  

---

