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 %}
<div class="form-group">
    <label for="fields[guestNames][{{ loop.index0 }}][col1]">Name {{ loop.index }}</label>
    <input id="fields[guestNames][{{ loop.index0 }}][col1]" type="text" name="fields[guestNames][{{ loop.index0 }}][col1]" class="form-control">
    {% if entry is defined %}{{ errorList(entry.getErrors('fields[guestNames]')) }}{% endif %}
{% 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 %}<input type="hidden" name="entryId" value="{{ entry.id }}">{% 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.

  • Absolutely brilliant, works perfectly. Thanks Andrew! Jan 7 '16 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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