markb
markb

Reputation: 1295

Loop between only 3 values

I'm trying to loop a class for each div.

I know how to get the first and last, but can't figure out how to iterate 1-3 (or more for others who want to expand this)

What I have:

$cats = get_categories();
// default value
$i = 0;

foreach ($cats as $cat) {
    // reset class
    $class = '';

    if(++$i % 3 == 0) { $class = 'third - '; }
    elseif(++$i % 2 == 0) { $class = 'second - '; }
    elseif(++$i % 1 == 0) { $class = 'first - '; }

    echo $class . $cat->name;
}

However, when I echo the $i to see what is happening, it is adding values each time, rather than going through the if statement (which I thought would not cause it to increase the number)

I end up getting this kind of output:

third  - cat01
first  - cat02
second - cat03
third  - cat04
second - cat05
first  - cat06
third  - cat07

Upvotes: 3

Views: 1151

Answers (5)

Anish Abraham
Anish Abraham

Reputation: 237

Your code $i increment every Checking of condition, check the below code,

$cats = get_categories();
    // default value
    $i = 0;

    foreach ($cats as $cat) {
        // reset class
        $class = '';
        $i++;//increment outside
        if($i % 3 == 0) { $class = 'third - '; }
        elseif($i % 2 == 0) { $class = 'second - '; }
        elseif($i % 1 == 0) { $class = 'first - '; }

        echo $class . $cat->name;
    }

the Output is

first  - cat01
second - cat02
third - cat03
first  - cat04
second - cat05
third - cat06
first  - cat07

otherwise you use below for loop

foreach ($cats as $cat) { 
 $i++;
 if ($i % 3 == 0) { $class = ' third -'; } 
 elseif ($i % 3 == 1) { $class = ' first -'; } 
 elseif ($i % 3 == 2) { $class = ' second -'; } 
 echo $class . $cat->name;

   } 

the calcuation of mod

multiple of 3 % 3 =0
((multiple of 3)-1) % 3 =2
((multiple of 3)-2) =1

example

4%3=1
5%3=2
6%3=0

Upvotes: 0

Major problem is at $i % 2 == 0 . It should be $i % 3 == 1. You must always extract modulus 3 and check the result against 0, 1 and 2:

$cats = array('cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7');
$i = 0;
foreach ($cats as $cat) {
    if ($i % 3 == 0) { $class = ' first'; }
    elseif ($i % 3 == 1) { $class = ' second'; }
    elseif ($i % 3 == 2) { $class = ' third'; }
    $i++;
    echo "\n$class - $cat";
}

Output:

 first - cat1
 second - cat2
 third - cat3
 first - cat4
 second - cat5
 third - cat6

http://ideone.com/mljRcn

With 4 classes it works too:

http://ideone.com/fLR2nl

Upvotes: 3

evansgambit
evansgambit

Reputation: 905

$sequence = ['first', 'second', 'third'];

for($i = 0; $i < count($cats); $i++)
{
    $class = current($sequence) . $cats[$i]->name;

    if (next($sequence) === false)
    {
        reset($sequence);
    }
}

You can do the same with foreach.

foreach ( $cats as $cat )
{
    $class = current($sequence) . $cat->name;

    if (next($sequence) === false)
    {
        reset($sequence);
    }
}

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

I believe you can achive what you want in css alone when it comes to assigning a class every n rows and repeat.

For example, the following php simply generates some simple div elements in a loop with no classes assigned inline - the colouring of the divs ( or whatever css rules you wish to apply ) is governed by the css rules below using the nth-child( 3n + x ) selector mechanism - here the colours/styles repeat every 3 elements but you can modify that to accomodate a different repetition interval - for a repetion every 4 child divs it would be nth-child( 4n + x ) where x is the child etc

echo "
<div id='parent'>";

for($i=0;$i<12;$i++){
    echo "
    <div>Red</div>
    <div>White</div>
    <div>Blue</div>";
}

echo "
</div>

<style>
    #parent > div:nth-child(3n+1){background:red;}
    #parent > div:nth-child(3n+2){background:white;}
    #parent > div:nth-child(3n+3){background:blue;}
</style>";




<?php
    $cats = get_categories();
    echo "<div id='parent'>"
    foreach( $cats as $cat ) {
        echo "<div>".$cat->name."</div>";
    }
    echo "</div>";
?>

<style>
    #parent > div:nth-child(3n+1){background:red;}
    #parent > div:nth-child(3n+2){background:white;}
    #parent > div:nth-child(3n+3){background:blue;}
</style>

Upvotes: 0

Julian T
Julian T

Reputation: 11

Check out php foreach loop document here http://php.net/manual/en/control-structures.foreach.php

You can use your foreach loop like this :

foreach ($cats as $key => $cat) {}

$key would be your index.

Upvotes: 0

Related Questions