pealo86 Posted May 23, 2012 Share Posted May 23, 2012 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? Quote Link to comment Share on other sites More sharing options...
Jamdog Posted May 23, 2012 Share Posted May 23, 2012 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... Quote Link to comment Share on other sites More sharing options...
DavidAM Posted May 23, 2012 Share Posted May 23, 2012 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 } Quote Link to comment Share on other sites More sharing options...
pealo86 Posted May 23, 2012 Author Share Posted May 23, 2012 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? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 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? Quote Link to comment Share on other sites More sharing options...
Jamdog Posted May 23, 2012 Share Posted May 23, 2012 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)) { Quote Link to comment Share on other sites More sharing options...
pealo86 Posted May 23, 2012 Author Share Posted May 23, 2012 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? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 Are you incrementing $count? Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 23, 2012 Share Posted May 23, 2012 $count = 1; while($count < 20) { if ($count == 1 || $count % 4 == 0) { echo $count . ', '; } $count++; } This outputs: 1, 4, 8, 12, 16 Is that right? Quote Link to comment Share on other sites More sharing options...
pealo86 Posted May 25, 2012 Author Share Posted May 25, 2012 Are you incrementing $count? Just tested in a basic PHP file and it seems to work! There must be something in my WP loop that I haven't spotted so I'll look into it further. Thanks again! Quote Link to comment Share on other sites More sharing options...
pealo86 Posted May 25, 2012 Author Share Posted May 25, 2012 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? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 25, 2012 Share Posted May 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
pealo86 Posted May 31, 2012 Author Share Posted May 31, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 31, 2012 Share Posted May 31, 2012 <?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 Quote Link to comment Share on other sites More sharing options...
pealo86 Posted June 8, 2012 Author Share Posted June 8, 2012 <?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? :/ Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 8, 2012 Share Posted June 8, 2012 You didn't do the final else of the if else statement. Quote Link to comment Share on other sites More sharing options...
pealo86 Posted June 9, 2012 Author Share Posted June 9, 2012 Ahhh yes! I didn't think it would be needed in my WP loop, but obviously it is! Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.