0

I'd like to set the following up as a loop (so that the pattern is repeated every 5 elements). I've tried to read up on it and I believe I need to set it up as an array, but I have no idea where to start.

Here's my code:

<div class="ccm-page-list">

    <?php

        $i= 0;i;
        foreach ($pages as $page):
            $i++;

            $title  = $th->entities($page->getCollectionName());
            $url    = $nh->getLinkToCollection($page);
            $target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
            $target = empty($target) ? '_self' : $target;

            $description = $page->getCollectionDescription();
            $description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
            $description = $th->entities($description); 

            $img   = $page->getAttribute('thumbnail');
            $thumb = $ih->getThumbnail($img, 550, 550, true);

    ?>

    <a href="<?php  echo $url ?>" target="<?php  echo $target ?>"> <div class="col-sm-4  grid-item">
        <div class="item-img-blog" >
            <img src="<?php  echo $thumb->src ?>" width="auto" height="100%" alt="" />
            <div <?php 
                if($i == 1) {
                ?>class="item-class-1" <?php } 
                if($i == 3) {
                ?>class="item-class-2" <?php } 
                if($i == 2) {
                ?>class="item-class-3" <?php } 
                if($i == 4) {
                ?>class="item-class-3" <?php } 
                if($i == 5) {
                ?>class="item-class-1" <?php } 
            ?>>

                <div class="text-container">
                    <h1><?php  echo $description ?></h1>
                    <p><?php  echo $date = $page->getCollectionDatePublic('d/m/Y'); ?></p>
                </div>
            </div>
        </div>
    </div></a>
    <?php  endforeach; ?>
</div> 

I would really appreciate any help! Thanks in advance!

2
  • Am I correct in thinking that instead of using $i as 1 2 3 4 5 6 7 8 9 you actually would like 1 2 3 4 5 1 2 3 4 5 and so on? Commented May 8, 2014 at 16:52
  • Yes that's exactly what I'd like to do! Sorry, I'm in a bit over my head! Commented May 8, 2014 at 16:56

2 Answers 2

1
$i= 0;
$classes = array(1 => 1, 2 => 3, 3 => 2, 4 => 3, 5 => 1);
foreach ($pages as $page) {
  $i++;
  .....
  echo 'class="item-class-'.$classes[$i % (count($classes) + 1)];    
  .....
}
1

You don't need an extra loop, you can use the modulo operator (aka, the remainder of a division) and some basic math to cope with the fact that you want to cycle 1->5:

$i= 0;   // note that I removed the "i;" at the end of this line!
foreach ($pages as $page):
   $cyclici = ($i % 5) + 1;
   // then, use $cyclici everywhere you want to see that 1-5 1-5 pattern
   ...
   if($cyclici == 1) {
   ...

   // and increment $i at the *end* of the loop instead of the start.
   $i++
endforeach;

EDITed to clarify the relationship between $i and $cyclici - $i is still incremented for each iteration of the loop, $cyclici is derived from that incrementing value to obtain the desired 1 2 3 4 5 1 2 3 4 5 ... sequence.

2
  • This did it! Thanks very much! (no need to clarify) :) Commented May 9, 2014 at 9:50
  • I think the clarification can still help future visitors, so I'll leave it in place. Glad I could help you solve your problem :) Commented May 9, 2014 at 9:54

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.