0

I've got a forech loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.

    <div class="autogrid_wrapper cte block">
        <div class="inner">
            <?php foreach($this->entries as $entry): ?>
                <figure class="image_container">
                    <img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
                </figure>
            <?php endforeach; ?>
        </div>
</div>

I hope you guys can help me.

2
  • With the div.inner? Also missing a closing div. Commented Apr 6, 2016 at 9:47
  • Yes, the div.inner should also be created new, with the div.autogrid_wrapper. Commented Apr 6, 2016 at 9:51

1 Answer 1

3

A simple counter could help -

<div class="autogrid_wrapper cte block">
    <div class="inner">
        <?php 
        $i = $j = $k = 0;
        foreach($this->entries as $entry): 
        $i++;
        $class = '';
        if($j === 0) {
            $class = 'first';
        }
        $j++;
        $html = '';
        if($i % 5 === 0) {
            $k++;
            $j = ($i - (5 * $k));
            $class = 'last';
            $html = "</div></div>
                 <div class='autogrid_wrapper cte block'><div class='inner'>";
        } 
        ?>
            <figure class="image_container <?php echo $class; ?>">
                <img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
            </figure>
        <?php 
        echo $html;
        endforeach; 
        ?>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot man. One little thing, is it possible to give the first figure.image_container the class .first and the last figure.image_container the class .last?
Wow and how can I get the class .last on the last element of the 5 elements?
When I pu if($i === 1) { $class = 'first'; } it works for the first 5 elements but not for the following.
Now it works for the class .last but the class .first isn't displayed.
Now the first 4 elements are always .first.^^
|

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.