tags:

views:

56

answers:

4

I have a foreach that looks like this:

                          foreach ($blogusers as $bloguser) {
                        $args = array(
                        'author' => $bloguser->user_id,
                          'showposts' => 1,
                          'caller_get_posts' => 1
                        );
                        $my_query = new WP_Query($args);
                        if( $my_query->have_posts() ) {
                          $user = get_userdata($bloguser->user_id);
                          userphoto($bloguser->user_id, "<div class='all_authors'><a href='http://blogg.nacka.se/nastasteg/author/".$user-&gt;user_login . "'>","</a><ul><li><a href='http://blogg.nacka.se/nastasteg/author/".$user-&gt;user_login . "'>" .$user->user_firstname."</a></li><li class='occupation'>".$user->user_lastname."</li></ul></div>", array('width' => 135, 'height' => 135));
                          #echo "<div class='all_authors'><a href='http://blogg.nacka.se/nastasteg/author/".$user-&gt;user_login . "'><img src='http://www.gravatar.com/avatar/" . md5( strtolower( trim( " $user->user_email " ) ) )."?s=135' /></a><ul><li><a href='http://blogg.nacka.se/nastasteg/author/".$user-&gt;user_login . "'>" .$user->user_firstname."</a></li><li class='occupation'>".$user->user_lastname."</li></ul></div>";
                        }
                      }

In every foth div, Would like to add an extra class. How do I do this?

+6  A: 

Create a variable before your foreach called $i and set it to 0;

Inside you foreach use

$class = ($i%4 === 0) ? 'yourclass' : '';

$class is now either 'yourclass' or an empty string

Then at the end of your foreach increment $i with $i++

Alex
+1 for using modulus operator
Drackir
A: 

I would look at creating a local variable before the loop, incrementing on each loop iteration, on significant loop iteration perform actions and reset to 0

aking1012
A: 

Add counter to it perhaps?

$counter = "";
foreach($array AS $variable){
  $counter +=1;
  // Here you will do your standard stuff you do always
  if($counter == 4){  // check for right counter value
    $counter = "";  // null the counter again
    // HERE you can do stuff that only happens every 4th iteration
  }
  // HERE you can carry on with your usual stuff that happens always
}

Of course there are many different ways to do this, this is in my eyes the simples one.

mistrfu
Might be a pet peeve, but `""!==NULL`. if it is a counter setting it to `0` is more appropriate. It will work of course, no question about it.
Wrikken
You are correct. I know it is a little dirty :) I tend to do it because for some reason when going through my code later, seeing "" is somewhat easier for me to find that 0.I guess I should try to be "cleaner" at least here when helping other people ;) Thanks for reminding me.
mistrfu
+1  A: 

Use the modulus operator. I see @Alex has beaten me to the mark with this, but I offer this code I wrote and tested, so that others can see more clearly the principle:

$blogusers=array('a','b','c','d','e','f','g','h','i','j');
$i=0;
foreach ($blogusers as $bloguser) {
    if($i % 4 === 0) $extraclass= "fourthClass";
    $resultHTML .= "<div class=\"standardClass $extraclass\">$bloguser</div>";
    $i++;
    $extraclass="";
}
echo $resultHTML;

Could be made more compact with the ternary operator, but this is the principle.

norwebian