hairynuggets
hairynuggets

Reputation: 3311

How to output a value on every third result of a foreach statement in php?

I have a foreach statement in my app that echos a list of my database results:

<?php

foreach($featured_projects as $fp) {
  echo '<div class="result">';
  echo $fp['project_name'];
  echo '</div>';
}

?>

I would like to:

On every third result, give the div a different class. How can I achieve this?

Upvotes: 10

Views: 13585

Answers (11)

Treffynnon
Treffynnon

Reputation: 21553

You can use a counter and the modulo/modulus operator as per below:

<?php

// control variable
$counter = 0;

foreach($featured_projects as $fp) {

    // reset the variable
    $class = '';

    // on every third result, set the variable value
    if(++$counter % 3 === 0) {
        $class = ' third';
    }

    // your code with the variable that holds the desirable CSS class name
    echo '<div class="result' . $class . '">';
    echo $fp['project_name'];
    echo '</div>';
}

?>

Upvotes: 22

dAm2K
dAm2K

Reputation: 10339

This is the working version, sorry for my prev version:

<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";

$class[0] = "class1";
$class[1] = "class2";

$i=0;
foreach($featured_projects as $fp) {
  $i++;
  if ($i == 3) {
    $c = $class[1];
    $i=0;
  } else {
    $c = $class[0];
  }
  echo "<div class=\"$c\">";
  echo $fp['project_name'];
  echo "</div>\n";
}
?>

Produces:

<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>

Upvotes: 0

Pushparaj
Pushparaj

Reputation: 538

You can add a counter in loop ...try the following...

 <?php 
 $i = 0; 
 foreach($featured_projects as $fp) {
 $i = ++$i;
 if(($i%3) == 0)
 {
  $class1 = 'test1';
 }
 else
 {
 $class1 = 'test2';
 }
       echo '<div class="'.$class1.'">';
       echo $fp['project_name'];
       echo '</div>';
 }
 ?>

Upvotes: 0

ab_dev86
ab_dev86

Reputation: 1982

<?php

foreach ($featured_projects as $i => $fp) {
    echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
    echo $fp['project_name'];
    echo '</div>';
}
?>

Upvotes: 6

blaff
blaff

Reputation: 304

<?php
    $counter = 0;

    foreach ($featured_projects as $fp) {
        echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
        echo $fp['project_name'];
        echo '</div>';
    }
?>

Upvotes: 0

user1129682
user1129682

Reputation: 1091

What leaves your code mostly in tact would be

<?php 
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>

But you may want to consider using a for or while construct around "each($featured_projects)" (see http://php.net/manual/en/function.each.php) which may result in neater code.

Upvotes: 0

Andrejs Cainikovs
Andrejs Cainikovs

Reputation: 28454

<?php
$i = 0;
foreach($featured_projects as $fp) {
    echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
    echo $fp['project_name'];
    echo '</div>';
}
?>

Upvotes: 0

jkgeyti
jkgeyti

Reputation: 2404

<?php 
foreach($featured_projects as $fp) {
    if(++$i % 3 === 0) {
        $class = ' something';
    } else {
        $class = '';
    }
    echo '<div class="result' . $class . '">';
    echo $fp['project_name'];
    echo '</div>';
}
?>

Upvotes: 0

jazzytomato
jazzytomato

Reputation: 7214

Using a counter and modulo operator this is easy to implement

Upvotes: 0

Daan Geurts
Daan Geurts

Reputation: 449

add a counter in this loop and check if counter equals three and apply class.

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72662

If the $featured_projects array is based on incremental index you could simply use the index and the modulo % operator.

Otherwise you would have to add a counter.

http://php.net/manual/en/language.operators.arithmetic.php

Upvotes: 1

Related Questions