Foreach, special processing of every nth element (odd, for example, for example)

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->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 each foth div, I would like to add an additional class. How should I do it?

+3
source share
4 answers

Use the module operator. I see that @Alex beat me to such an extent, but I suggest this code that I wrote and tested so that others can more clearly see 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;

It can be made more compact with a ternary operator, but that’s the principle.

+12
source

Create a variable in front of your foreach named $ i and set it to 0;

Inside you use foreach

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

$class now either "yourclass" or an empty string

foreach $i $i++

+7

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

0
source

Add a counter to it?

$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, it is simple in my eyes.

0
source

Source: https://habr.com/ru/post/1771299/


All Articles