kesm0
kesm0

Reputation: 875

Different html class every 2 item

In php I try to do this :

<div class="left"> some text </div>
<div class="left"> some text </div>
<div class="right"> some text </div>
<div class="right"> some text </div>
<div class="left"> some text </div>
<div class="left"> some text </div>
<div class="right"> some text </div>
<div class="right"> some text </div>
.
.
.

I need different class every 2 items to have different html design.

Upvotes: 0

Views: 116

Answers (3)

hurricane
hurricane

Reputation: 6724

Just try this,

  $arrayX = array("left","right");
    $i = 4;
    while($i<100){
        if($i%4<2){
            echo '<div class="'.$arrayX[0].'"> some text </div>';
        }else{
            echo '<div class="'.$arrayX[1].'"> some text </div>';
        }
        $i++;
    }

Upvotes: 2

Dean Jenkins
Dean Jenkins

Reputation: 304

Presumably you have a foreach loop that goes through all the 'some text' entries. You could add a counter and then do some simple logic.

$yourtexts = array("some text","some text","some text","some text","some text","some text","some text","some text","some text");

$i=0; // counter
foreach ($yourtexts as $sometext) {
    print ($i<2) ? '<div class="left">' : '<div class="right">'; // $i<2 go left else go right
    print "$sometext</div>";
    $i++; // count
    if ($i>3) { $i=0; } // reset to zero.
}

Upvotes: 1

keja
keja

Reputation: 1363

Maybe you can use this CSS instead.

div:nth-child(4n+1), 
div:nth-child(4n+2) 
{
    background: #ff0000;
}

Upvotes: 1

Related Questions