0

Thanks in advance to anyone who can help me :)

I have a table field in Craft that my template is pulling in data from... the issue is that in the box at the start of each row I want a different image.

Every time the loop goes round and pulls in the data for each row from the table in Craft I also want it to pull in an image using craft.assets.folderId().

Here is my code so far.. it pulls in the individual images but it repeats the the first row of the table for each image... and then it pulls in all the the images again repeating the next row and so on... please see this link to get an idea of what I mean: http://code7dev11.co.uk/how-it-works

{% if entry.howItWorksTable | length %}
        <table>
            {% for row in entry.howItWorksTable %}
                {% for image in craft.assets.folderId('2') %}
                <tr>
                    <td><img src="{{ image.getUrl() }}">{{ row.stages }}</td>
                    <td>{{ row.skillsLearnt }}</td>
                    <td>{{ row.classes }}</td>
                    <td>{{ row.price }}</td>
                </tr>  
                {% endfor %}
            {% endfor %}
        </table>
{% endif %}
1
  • Wouldn't it be easier to just use Matrix for this? That way you can link directly to the images that you want using a proper Assets field.
    – Brad Bell
    Jul 8 '15 at 17:36
1

Here is what I would do:

First, make an actual array of all the images (using find to make the array):

{% set imageArray = craft.assets.folderId('2').find() %}

Then, loop over the entries:

{% for row in entry.howItWorksTable %}

and in that loop each time you display a row, you can get the corresponding image (using the loop variable index0):

{% set image = imageArray[loop.index0] %}

Putting it all together:

{% set imageArray = craft.assets.folderId('2').find() %}

{% if entry.howItWorksTable | length %}
  <table>
    {% for row in entry.howItWorksTable %}
      <tr>
        <td><img src="{{ image.getUrl() }}">{{ row.stages }}</td>
        <td>{{ row.skillsLearnt }}</td>
        <td>{{ row.classes }}</td>
        <td>{{ row.price }}</td>
      </tr>  
    {% endfor %}
  </table>
{% endif %}
1
  • I was wondering where the looped image index is specified within the code above?
    – buck1112
    Jul 27 '18 at 17:42

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.