Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
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? –  fvu May 8 at 16:52
    
Yes that's exactly what I'd like to do! Sorry, I'm in a bit over my head! –  user3617265 May 8 at 16:56

2 Answers 2

$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)];    
  .....
}
share|improve this answer

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.

share|improve this answer
    
This did it! Thanks very much! (no need to clarify) :) –  user3617265 May 9 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 :) –  fvu May 9 at 9:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.