开发者

Foreach, special treatment of every n:th item (odd, even for example) [duplicate]

This question already has answers here: PHP: How do you determine every Nth iteration of a loop? (8 answers) Closed 3 years ago.

I have a foreach that looks like this:

                          foreach ($blogusers as $bloguser) {
                        $args = array(
                        'author' => $bloguser->user_id,
 开发者_如何学Go                         '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->user_login . "'>","</a><ul><li><a href='http://blogg.nacka.se/nastasteg/author/".$user->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->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->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?


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.


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++


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜