<p>The problem that you expect your <code>qty</code> variable to be an array of guests and initialize it like that, but once you set a quantity it will be an integer, so the loop won't work correctly. See this line:</p>
<pre class="lang-twig prettyprint-override"><code>{% set qty = qty ?? [['']] %}
</code></pre>
<p>This initializes <code>qty</code> to an array containing another array containing an empty string (by the way, why do you need the nested array? Wouldn't a simple array of guest names suffice?). But once you select a quantity, the <code>qty</code> variable will simply be an integer (or numeric string, not sure). A for-loop expects an iterable (array or iterable object), so your for-loop is invalid:</p>
<pre class="lang-twig prettyprint-override"><code>{% set qty = 3 %}

{# this is invalid #}
{% for guest in qty %}
    {# this will never be reached #}
{% endfor %}
</code></pre>
<p>Instead of <code>qty</code>, what you want is a <code>guests</code> variable which holds a number of items in your expected format (in this case, arrays of strings) corresponding to the selected quantity. For example:</p>
<pre class="lang-twig prettyprint-override"><code>{% set qty = qty ?? 1 %}
{% set guests = range(0, qty - 1)|map(i =&gt; ['']) %}

{% for guest in guests %}
  &lt;input sprig type=&quot;text&quot; name=&quot;options[guests[{{ loop.index0 }}][0]]&quot;&gt;
{% endfor %}
</code></pre>
<p>Note this will reset the values in the guest input fields whenever the quantity is changed. You'd need to retrieve the current inputs as well to set the current values of the input fields as well.</p>

CC BY-SA 4.0
2021-03-31T03:26:23.547Z
That did it - thank you!
supazu
CC BY-SA 4.0
2021-03-26T11:48:32.373Z
2021-10-22T07:30:59.643Z
2021-10-22T07:30:59.643Z
MoritzLost
<p>I'm using Commerce to sell tickets to an event and need to get the names of each person. I would like to do this via a Matrix field, showing fields based on the quantity selected.</p>
<p>I followed along with the new Sprigform tutorial, which covers part of it but it has an add button as opposed to a dropdown.</p>
<p>This is what I have so far, but on changing the select value the input field disappears, so maybe I'm just looping over things incorrectly?</p>
<pre><code>{% set qty = qty ?? [['']] %}
&lt;select sprig name=&quot;qty&quot;&gt;
  {% for i in range(1, 5) %}
    &lt;option value=&quot;{{ i }}&quot; s-val:qty=&quot;{{ i }}&quot; {{ qty == i ? 'selected' }}&gt;{{ i }}&lt;/option&gt;
  {% endfor %}
&lt;/select&gt;
    
{% for guest in qty %}
  &lt;input sprig type=&quot;text&quot; name=&quot;options[guests[{{ loop.index0 }}][0]]&quot;&gt;
{% endfor %}
</code></pre>
<p>Would appreciate any pointers!</p>

CC BY-SA 4.0
2021-03-14T23:53:28.767Z
Still massively struggling with this one. Am I just not understanding for loops properly?
supazu
<matrix><plugin-sprig>
Show matrix fields based on quantity [Sprig]
# Show matrix fields based on quantity [Sprig]

(Asked by: username_0 on 2021-03-08)

I'm using Commerce to sell tickets to an event and need to get the names of each person. I would like to do this via a Matrix field, showing fields based on the quantity selected.
I followed along with the new Sprigform tutorial, which covers part of it but it has an add button as opposed to a dropdown.
This is what I have so far, but on changing the select value the input field disappears, so maybe I'm just looping over things incorrectly?
{% set qty = qty ?? [['']] %}

  {% for i in range(1, 5) %}
    {{ i }}
  {% endfor %}

    
{% for guest in qty %}
  
{% endfor %}

Would appreciate any pointers!

**Tags:** matrix, plugin-sprig

> **Comments:**
> **username_0**: Still massively struggling with this one. Am I just not understanding for loops properly?  

---

## 1 answers

### Answer by username_1 on 2021-03-26. Score: 2 | Accepted answer

The problem that you expect your qty variable to be an array of guests and initialize it like that, but once you set a quantity it will be an integer, so the loop won't work correctly. See this line:
{% set qty = qty ?? [['']] %}

This initializes qty to an array containing another array containing an empty string (by the way, why do you need the nested array? Wouldn't a simple array of guest names suffice?). But once you select a quantity, the qty variable will simply be an integer (or numeric string, not sure). A for-loop expects an iterable (array or iterable object), so your for-loop is invalid:
{% set qty = 3 %}

{# this is invalid #}
{% for guest in qty %}
    {# this will never be reached #}
{% endfor %}

Instead of qty, what you want is a guests variable which holds a number of items in your expected format (in this case, arrays of strings) corresponding to the selected quantity. For example:
{% set qty = qty ?? 1 %}
{% set guests = range(0, qty - 1)|map(i => ['']) %}

{% for guest in guests %}
  
{% endfor %}

Note this will reset the values in the guest input fields whenever the quantity is changed. You'd need to retrieve the current inputs as well to set the current values of the input fields as well.

> **Comments:**
> **username_0**: That did it - thank you!  

---

