1

I'm trying to get a count inside a loop of a Matrix field. Normally I could just do:

{field}
  {row_count}
{/field}

And BAM, that would work perfectly. But not in this case. Because I need to do something like this:

{field}
  File 1: {image_field_1}
  Count: {row_count}
  {if image_field_2}
   File 2: {image_field_2}
   Count: ?!
  {/if}
  ------
{/field}

Which would output something like:

File 1: images/bobafett.jpg
Count: 1
------
File 1: images/lukeskywalker.jpg
Count: 2
------
File 1: images/jabba.jpg
Count: 3
------
File 1: images/darth.jpg
Count: 4
File 2: images/obiwan.jpg
Count: !?
------
File 1: images/r2d2.jpg
Count: 5
------

I've tried doing this with PHP:

<?php $count = 1; ?>

{field}
  File 1: {image_field_1}
  Count: <?php echo $count; $count++; ?>
  {if image_field_2}
   File 2: {image_field_2}
   Count: <?php echo $count; $count++; ?>
  {/if}
  ------
{/field}

But I think because of parse order, even entries that don't contain {image_field_2} render the PHP, so the count goes completely out of sync:

File 1: images/bobafett.jpg
Count: 1
------
File 1: images/lukeskywalker.jpg
Count: 3
------
File 1: images/jabba.jpg
Count: 5
------
File 1: images/darth.jpg
Count: 6
File 2: images/obiwan.jpg
Count: 7
------
File 1: images/r2d2.jpg
Count: 8
------

I've tried changing PHP to Output and Input and Output works best for me, because Input just parses '1' for the count every time. I think mainly down to parse order again.

I tried looking for a small plugin to do something like this for me, that would stop PHP being in the template AND would sort any parse order issues.

help

0

Go with Matt's JS idea. It's easily done with jQuery:

Here's working code for you. You'll just need to adjust your selectors.
http://jsfiddle.net/gxFdH/1/

| improve this answer | |
  • 1
    JS isn't out of the question, mainly because the count is for a slideshow which runs through JS anyway, so I might be able to take this route. – Jason Mayo Sep 27 '13 at 18:44
  • Quickest way IMO. – Anna_MediaGirl Sep 27 '13 at 19:19
1

Jason, is JavaScript totally out of the picture for this?

Also, what's your PHP expertise like? I've solved similar issues in the past using a custom plugin and a static PHP variable. Basically, you can try setting the channel:entries field around the matrix to parse="inward", so it runs first, and have a plugin with a method that outputs and increments a static variable each time it's run. Parsing the outer loop first should help make sure your plugin only runs on actual output.

One tip is you'll want to add the "random" parameter (with no value) to your custom plugin tag, to make sure EE doesn't cache its output. Here's some pseudo-code around that:

public function current_count()
{
    static $count = 0;
    $count++;
    return $count;
}
| improve this answer | |
  • My PHP skills are okay - I'm just not the best at starting plugins like this from scratch. I might have a go at this though and see how I get on. I've also tried the parse inward on many elements but it still didn't work sadly. – Jason Mayo Sep 27 '13 at 18:43

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.