3

I have a matrix field where the client can add as many rows as they like.

The rows get output to a 2 column layout:

<div class="col1">

    <p>Matrix row 1</p>

    <p>Matrix row 2</p>

</div>
<div class="col2">

    <p>Matrix row 3</p>

    <p>Matrix row 4</p>

</div>

So the template code would be:

{matrix_field}

<div class="col1">

    <p>{matrix_data}</p>

{if row_count == (total_rows / 2)}
</div>
<div class="col2">
{/if}

{/matrix_field}

</div>

Of course {if row_count == (total_rows / 2)} doesn't exist. How can this be achieved?

2
1

Using grid and native (2.9.2) it’s quite cool, you can now do could do:

{if grid_field:count == grid_field:total_results / 2}

In our case I wanted to create a two column set of lists :)

3

{if row_count == total_rows / 2} is not exist

actually, is exist - from version 2.9.0. I tested it with EE v2.9.2 & Matrix 2.6

Mathematic Operators

example for your case - split to 2 columns:

{if (total_rows % 2 == 0 && total_rows / 2 == row_count) || (total_rows % 2 == 1 && (total_rows+1) / 2  == row_count)}
                    <hr>
{/if}

available mathematic operators

+           Addition
-           Subtraction / Negation
*           Multiplication
** and ^    Exponentiation
/           Division
%           Remainder of one number divided by another

For other cases where you need to calculate something u can use MX Calc add-on - it's free.

2
  • Thanks Max, but that's EE code not Matrix, it won't work. I need to know the Matrix row_count not count. It would be great if P&T added those operators. Jan 7 '15 at 10:30
  • Matrix use native condition engine. I just tested with EE v2.9.2 & Matrix 2.6 - it works. Only one issue can be if total_rows is odd (is no round function here). But I add condition example into my answer which can fit this case. update: do u have rows from the same entry or from multi?
    – Max Lazar
    Jan 7 '15 at 22:46
2

I've done that before using the simple math add-on to do the calculation of where to break the matrix rows into their respective columns. Basically just plug in the matrix total rows variable, device by two and round up if you can (so that if there is an uneven number, the first column gets the extra one) and then use a conditional to insert the closing of column one and opening of column 2 (by comparing the result of the path to the current row count), and similarly use a conditional to close the second column. I don't recall if the Simple Math add-on actually supports rounding in any particular direction though - but there's more than one math add-on for EE that you may consider.

1
  • Thanks Jean St-Amand. A 'quick/dirty' way is to turn PHP on for the template and just do the Math in PHP. Taking it further I could make a plugin like simple math. The division couldn't be easier! I was hoping for a native EE or Matrix solution. One issue might be parse order, I'll give it a go and post back. Dec 8 '14 at 10:21

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.