1

Is there a possibility to increment a variable/count (something like "i++") within a matrix field so that the variable is incremented for each row with certain parameters?

The background is that I created a Matrix field to allow the user to reorder html elements. We use Channel Images to upload

I'm trying to achieve this in my template:

{my_matrix}

 {if cell_type == "image"}
 <!-- insert image1.jpg, image2.jpg, image"i++".jpg -->
   {exp:channel_images:images_static entry_id="{entry_id}"}
     <img src="{image:{i++}:url}">
   {/exp:channel_images:images_static"}
 {/if}

 {if cell_type == "text"}
   {text_from_matrix_cell}
 {/if}

{/my_matrix}

The html code is supposed to be looking like this:

<img src="image1.jpg">
<p>Text block 1…</p>
<img src="image2.jpg">
<p>Text block 2…</p>
<p>Text block 3…</p>
<img src="image3.jpg">

I tried it with a switch:

{my_matrix}

 {if cell_type == "image"}
   {exp:channel_images:images_static entry_id="{entry_id}"}
     <img src="{image:{switch'1|2|3|4|5'}:url}">
   {/exp:channel_images:images_static"}
 {/if}

 …

{/my_matrix}

The switch statement is working between matrix rows. However, it's alternating between matrix rows whether the surrounding "if"-statement applies or not. So instead of the desired result posted above, my actual html looks like this:

<img src="image1.jpg">
<p>Text block 1…</p>
<img src="image3.jpg">
<p>Text block 2…</p>
<p>Text block 3…</p>
<img src="image6.jpg">

I couldn't think of a way to solve this with php either. Any ideas? Thanks!

1

How about something like this?

{matrix}
  {exp:channel_images:images_static entry_id="{entry_id}"}
    {image:{element_count}}
  {/exp:channel_images:images_static}
{/matrix} 

({element_count} is the matrix field where you can choose the #-th image).

| improve this answer | |

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.