Carpy
Carpy

Reputation: 1074

foreach loop - different class per item

I need to give a different class name to every 6 divs within a foreach loop. I thought my below idea would work, but every item seems to get the same div.

<?php $num = 0; ?>
<?php foreach ($divs as $div): ?>

    <?php if($num % 1 == 0): ?>
    <div class="threecol grey one">
    <?php elseif($num % 2 == 1): ?>
    <div class="threecol green two ">
    <?php elseif($num % 3 == 2): ?>
    <div class="sixcol blue last three">
    <?php elseif($num % 4 == 3): ?>
    <div class="threecol grey">
    <?php elseif($num % 5 == 4): ?>
    <div class="sixcol blue">
    <?php elseif($num % 6 == 5): ?>
    <div class="threecol green last">
    <?php endif; ?>

        Rest of html

    </div>

<?php $num++ ?>
<?php endforeach; ?>

To simplify i basically need

1st/7th/13th/19th...etc div to be <div class="threecol grey">

2nd/8th/14th/20th...etc to be <div class="threecol green">

3rd/9th/15th/21st...etc to be <div class="sixcol blue last">

4th/10th/16th/22nd <div class="threecol grey">

5th/11th/17th/23rd <div class="sixcol blue">

6th/12th/18th/24th <div class="sixcol blue">

Upvotes: 0

Views: 2975

Answers (4)

Majid Laissi
Majid Laissi

Reputation: 19788

<?php if(floor($num / 6) == 0): ?>
<div class="threecol grey one">
<?php elseif(floor($num / 6) == 1): ?>
<div class="threecol green two ">
<?php elseif(floor($num / 6) == 2): ?>
<div class="sixcol blue last three">
<?php elseif(floor($num / 6) == 3): ?>
<div class="threecol grey">
<?php elseif(floor($num / 6) == 4): ?>
<div class="sixcol blue">
<?php elseif(floor($num / 6) == 5): ?>
<div class="threecol green last">
<?php endif; ?>

This would give:

  • threecol grey one for columns from 0 to 5
  • threecol grey two for columns from 6 to 11
  • etc..

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

You are misunderstanding the modulo operator which results in the remainder of the dividend. Thus, % 1 will always be zero because there is never a remainder with that operation. You want to do this:

$num % 6 == 1
...
$num % 6 == 2

etc. You can also just do $num % 6 ahead of time and store it in a variable.

Upvotes: 1

Louis XIV
Louis XIV

Reputation: 2224

<?php foreach ($divs as $i => $div): ?>
    <?php if($i % 6 == 0): ?>
    <div class="threecol grey one">
    <?php elseif($i % 6 == 1): ?>
    <div class="threecol green two ">
    <?php elseif($i % 6 == 2): ?>
    <div class="sixcol blue last three">
    <?php elseif($i % 6 == 3): ?>
    <div class="threecol grey">
    <?php elseif($i % 6 == 4): ?>
    <div class="sixcol blue">
    <?php elseif($i % 6 == 5): ?>
    <div class="threecol green last">
    <?php endif; ?>
        Rest of html
    </div>
<?php endforeach; ?>

Consider use of a switch case statement.

Upvotes: 0

Marc B
Marc B

Reputation: 360702

Very ugly... why not

$classes = array('grey one', 'green two', 'blue last three', 'grey', 'blue', 'green last');
foreach($divs as $div) {
   $class = $classes[$i++ % 6];
   echo '<div class="threecol ' . $class . '">';
}

The main problem is that you're continually changing the divisor in your modulo operation, so you're always getting the 0 remainder.

Upvotes: 1

Related Questions