Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching iterations using php modulus in an irregular sequence

How can I write a modulus that will select the following iterations in sequence?

1, 4, 5, 8, 9, 12, 13 etc (+3+1r)

I'm working within a loop and counting posts (iterations).

So for example, I could catch every third post (1, 4, 7, 10) by:-

if ($i % 3 == 1) { echo 'something here'; } 

But how can I write one that will catch 1, 4, 5, 8, 9, 12, 13?

like image 467
zigojacko Avatar asked Jul 05 '26 14:07

zigojacko


1 Answers

I'm not quite sure about your algorithm, but it seems like you try to get every 3rd and 4th post not (starting at 0). The fitting code would be:

if(($i % 4 == 0 || $i % 4 == 1) && $i != 0) { /* do stuff */ }
like image 99
Sebb Avatar answered Jul 08 '26 05:07

Sebb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!