0

I'm trying to get the following layout using PHP

enter image description here

My code:

<div class="grid">

<?php
foreach( $rows as $i => $row ) :
  if ( $i % 3 == 0 ) :
    $size = "large";
  else :
    $size = "small";
  endif;
?>

  <div class="item <?php echo $size; ?>"></div>

<?php endforeach; ?>

</div>

This makes the first item large on each row. How can I make the first item large on the first row, the last item large on the second row, first item large on third row, and so on in that pattern, like in the layout example?

Note: I must get the layout in PHP using the $size variable, not using CSS selectors because there is more to the code. I use CSS only to set the width of large or small items.

2
  • What values are your $rows and $row variables? Commented Oct 31, 2019 at 17:20
  • 1
    @MaxVoisard that does not really matter. It's just a loop for getting values which do not matter in the demo Commented Oct 31, 2019 at 17:58

1 Answer 1

2

I think that's what your are looking for:

<div class="grid">
    <?php
    foreach( $entries as $i => $entry ) :
        $size = "small";

        if ( $i % 6 == 0 || $i % 6 == 5 ) :
            $size = "large";
        endif;
    ?>

        <div class="item <?php echo $size; ?>"></div>

    <?php endforeach; ?>
</div>

You just need to change your modulo number to 6 because you're stuff repeats every 6 entries. And then you just need to check if it's on the first position or the last position eq. 0 or 5.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.