Jump to content

Every 1st and 4th iteration of a loop


pealo86

Recommended Posts

I can use the modulus operator to target a specific iteration of a loop... other than the first! As dividing by '1' will obviously never give a remainder.

 

Here is the code I'm using:

 

<?php
// determine alpha / omega class
if ($count % 1 == 0) {
	// do this
}
elseif ($count % 4 == 0) {
	// do that
}
?>

 

Can anyone see another way around it?

 

Link to comment
Share on other sites

I don't see why you need the first 'if' - this does exactly the same as your code above:

<?php
// determine alpha / omega class

// do this

if ($count % 4 == 0) {
	// do that
}
?>

 

Another way of doing the same would be:

if (!($count % 4)) {

but either method is fine, and yours is easier for most coders to understand.

 

You also didn't specify a loop in your code at all, so I'm assuming it's there...

Link to comment
Share on other sites

I'm not sure what you mean by "every 1st". If you want special processing the VERY first time through the loop you would simply test for equality:

 

if ($count == 1) { // First time through
  // do something important
} elseif ( ($count % 4) == 0) { // Every 4th time
  // do something interesting
}

 

 

Link to comment
Share on other sites

Thanks, but that's not what I meant exactly.

 

I suppose a better way of putting it is to say every 4th iteration, but starting from the first if that makes sense?

What? Could you provide an example or example data in which you want to apply this loop?

Link to comment
Share on other sites

So, you want it at the 1st, 5th, 9th, 13th, etc...?

 

To do that, you would do something like:

if ($count % 4 == 1) {

or

if (($count - 1) % 4 == 0) {

or

if (!(($count - 1) % 4)) {

 

Thanks! That's what I mean yes, however those snippets of code seem to return true on iterations 1, 2 and 3 instead of just 1.

 

I should probably point out, $count starts off at 1 as opposed to 0. But I can't seem to get it to work either way?

Link to comment
Share on other sites

Hmmm okay this is confusing, I've simplified my WordPress loop like so:

<?php query_posts('post_type=team-member&posts_per_page=-1&orderby=title&order=ASC'); ?>
<?php if(have_posts()) : ?>
<?php $count = 1; ?>
<div id="feed-the-team" class="feed">
	<?php while(have_posts()) : the_post(); ?>
		<?php
			// determine alpha / omega class
			if ($count % 4 == 1) {
				$grid = 'alpha';
			}
			elseif ($count % 4 == 0) {
				$grid = 'omega';
			}
		?>
		<h2><?php echo $grid; ?></h2>

		<?php $count ++; ?>
	<?php endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>

 

Yet it's outputting the following:

alpha

alpha

alpha

omega

alpha

alpha

alpha

omega

alpha

alpha

alpha

omega

 

Am I missing something really obvious here?

Link to comment
Share on other sites

But having 1,5,9,13... gives you 4 initial instances of 'alpha' with 1 'omega', then 3 instances of 'alpha' with 1 instance of 'omega'.  This is what you want?  Seems irregular to me.

 

Or perhaps I'm still not understanding.  Can you post a desired output.

Link to comment
Share on other sites

But having 1,5,9,13... gives you 4 initial instances of 'alpha' with 1 'omega', then 3 instances of 'alpha' with 1 instance of 'omega'.  This is what you want?  Seems irregular to me.

 

Or perhaps I'm still not understanding.  Can you post a desired output.

 

Ahhh no, what I'd like is something like the following:

 

alpha

0 / false

0 / false

omega

alpha

0 / false

0 / false

omega

alpha

0 / false

0 / false

omega

alpha

0 / false

0 / false

omega

 

If you could let me know how to acheive this with the modulus operator that would be great :D

Link to comment
Share on other sites

<?php
$count = 1;
for($i=1; $i<=20; $i++){
if(($count-1)%4 == 0){
	echo 'Alpha';
}else if($count %4 == 0){
	echo 'Omega';
}else{
	echo 'False';
}
echo '<br />';
$count++;
}
?>

 

Result:

Alpha
False
False
Omega
Alpha
False
False
Omega
Alpha
False
False
Omega
Alpha
False
False
Omega
Alpha
False
False
Omega

Link to comment
Share on other sites

  • 2 weeks later...

<?php
$count = 1;
for($i=1; $i<=20; $i++){
if(($count-1)%4 == 0){
	echo 'Alpha';
}else if($count %4 == 0){
	echo 'Omega';
}else{
	echo 'False';
}
echo '<br />';
$count++;
}
?>

 

Result:

Alpha
False
False
Omega
Alpha
False
False
Omega
Alpha
False
False
Omega
Alpha
False
False
Omega
Alpha
False
False
Omega

 

Thank you! The script appears to work fine on it's own, however in my WordPress loop it behaves the same as the previous solution. In that it gives me

 

alpha

alpha

alpha

omega

alpha

alpha

alpha

omega

alpha

alpha

alpha

omega

 

I have no idea why this is! My WordPress loop is like so:

<?php query_posts('post_type=team-member&posts_per_page=-1&orderby=menu_order&order=ASC'); ?>
<?php if(have_posts()) : ?>
<?php $count = 1; ?>
<div id="feed" class="feed">
	<?php while(have_posts()) : the_post(); ?>
		<?php
			// determine alpha / omega class

			if (($count - 1) % 4 == 0) {
				$grid = 'alpha';
			} elseif ($count % 4 == 0) {
				$grid = 'omega';
			}
			echo '<br class="clear" />';
			echo '<p>' . $count . ' ' . $grid . '</p>';
			echo '<br class="clear" />';

		?>

		<?php $count ++; ?>
	<?php endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>

 

I'm not sure how familiar with WordPress anyone here is, but can you think of any reason why it might be doing this? :/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.