Get data from another loop from outside the current loop Sorry the question title is so random - feel free to correct it to something more succinct if you can think of one.

Related question: How to query for multiple relations with SuperTable

I have a front end Entry form which I'm using to create/edit an Entry which contains one field, a table called guestNames. The number of rows allowed in that table is set in another section's Entry (event). The SuperTable field in the event that defines how many rows are allowed is called guestAllocation. There two rows in that SuperTable, partner (Entries) and places (Number).

In my front end Entry form I want to create the correct number of input fields that are allowed. I have no problem doing that with this code:

{% for row in 1..entry.guestAllocation.relatedTo(partner).first.places %}
&lt;div class="form-group"&gt;
    &lt;label for="fields[guestNames][{{ loop.index0 }}][col1]"&gt;Name {{ loop.index }}&lt;/label&gt;
    &lt;input id="fields[guestNames][{{ loop.index0 }}][col1]" type="text" name="fields[guestNames][{{ loop.index0 }}][col1]" class="form-control"&gt;
    {% if entry is defined %}{{ errorList(entry.getErrors('fields[guestNames]')) }}{% endif %}
&lt;/div&gt;
{% endfor %}


For the purposes of this example, that could be a static number like 5. However, I also need to provide editing functionality rather than just creating a new entry each time, so with the following line:

{% if partnerGuestList|length %}&lt;input type="hidden" name="entryId" value="{{ entry.id }}"&gt;{% endif %} {# If there's a guest list for this partner, update it #}

I can check if the entry exists already. Now I need to provide the correct number of input fields from entry.guestAllocation.relatedTo(partner).first.places, but in those input boxes, I need to provide the data from the rows in the table partnerGuestList guestNames

To get the names I need, I can query:

{% for row in partnerGuestList.guestNames %}
    {{ row.guestName }}
{% endfor %}


Please comment if I can clarify any more!

Short version: How can I show data from an entry in a loop that isn't related to that entry?
Interesting. I'm not able to test this right now, but I think this should work...

You could first loop through the results, adding them to an array as you go...

{% set guestNameValues = [] %}

{% for row in partnerGuestList.guestNames %}
    {% set guestNameValues = guestNameValues|merge([row.guestName]) %}
{% endfor %}


And then within your loop that displays the fields set the value as...

{{ guestNameValues[loop.index0] }}


That should get the corresponding name from the array you previously set.