3

I want to select the last 2 rows in a matrix field: You would expect that this could be easily achieved by using the following code.

{matrix}
    {if row_count >=  total_rows - 1}
        test
    {/if}
{/matrix}


However, this throws a syntax error:

Parse error: syntax error, unexpected T_STRING in expressionengine/libraries/Functions.php(683) : eval()'d code on line 168


Using just "total_rows" works perfectly fine, adding the the "-1" causes the error. ( EE v 2.5.5 )

3
  • 2
    no math in templates or in conditionals. maybe with a math / calculate plugin.
    – GDmac
    Apr 23 '13 at 10:11
  • any idea which plugin I could use for this ?
    – janvl
    Apr 23 '13 at 12:20
  • ehm, maybe MX calculator or search on devot-ee
    – GDmac
    Apr 23 '13 at 13:58
2

TAking what Bhashkar Yadav suggested, you might now need to go with two matrix calls to achieve what yoku need.

{matrix orderby="row_id" sort="desc" limit="1" offset="1"}

{matrix_column}

{/matrix}

and

{matrix orderby="row_id" sort="desc" limit="1"}

{matrix_column}

{/matrix}

This should call the second last row first and then the last row. It's a little clunky, but I don't believe there is a better way to achieve what you need.

3
  • that should work, but there has to be an easier way. Are there any plugins that allow simple calculations in conditionals ?
    – janvl
    Apr 23 '13 at 12:21
  • 1
    searching Devot:ee pulls up a list of math plugins which may help. devot-ee.com/search/… I havne't used any of these myself, but hopefully one of them will do what you need.
    – CreateSean
    Apr 23 '13 at 13:35
  • You can use {previous_row} or {next_row} to get the second row.
    – foamcow
    Apr 24 '13 at 9:07
2

You could install MX Calculator and give something like this a go:

{matrix}
    {if row_count >= "{exp:mx_calc expression='{total_rows}-1'}"}    
        test
    {/if}
{/matrix}

What does that return?

0

Try this:

{matrix orderby="row_id" sort="desc" limit="2"}

{matrix_column}

{/matrix}
2
  • I need them do be in the right ( asc ) order..
    – janvl
    Apr 23 '13 at 9:34
  • Could you use {next_row}stuff{/next_row} BEFORE you output the content from the first row?
    – foamcow
    Apr 24 '13 at 10:22
0

How about something like this... Haven't tried this so don't know for sure it will work.

Basically, flip the matrix so you reverse the order then get the first 2 items.

{matrix_field sort="desc" limit="2"}
    stuff
{/matrix_field}

EDIT I see someone already suggested doing this, maybe combine with what I suggest below?

Or maybe you could make use the {next_row} and {previous_row} matrix tags

{matrix_field offset="{total_rows}"}
    {previous_row}Stuff here{/previous_row}
{/matrix_field}

Like I said, haven't tested this so it may well not work but hopefully it will give you some ideas.

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.