Page 1 of 1

how to begin an infinite loop, incremented daily

Posted: Mon Jan 05, 2009 11:30 am
by zachalbert
Hi, I am a rather newbish, self-taught programmer and I can't wrap my ahead around this problem. I think it involves nested loops, but I'm not sure.

The ultimate goal here is I am going to be creating an RSS feed based on a series of sequential writings (call it 5 for our purposes). I want the feed to display the first item on January 1st, the 2nd on the second, etc. But on January 6th, I want the feed to display the 1st item again. And so on til infinity.

Here's what I have so far:

Code: Select all

 
for ( $i = 0; $i <= 365; $i++ ) {
   echo $i . " - ";
   for ( $j = 0; $j <= 5; $j++) {
      echo $j . "<br />";
   }
}
 
Now this obviously doesn't work because the first count (the date) only increments every time the second count is finished. Ideally, what I want is for this display:

0 - 0
1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 0
7 - 1
8 - 2
...

I can't figure out how to make the first column of numbers begin at 0 and increment 1 for each day. Then the second column begins at 0 and increments until 5, then starts over.

I might be completely off in my approach. If it's completely confusing, refer to the goal stated above.

Re: how to begin an infinite loop, incremented daily

Posted: Mon Jan 05, 2009 11:47 am
by xpgeek
I have a bad headache now :(
I am sorry for that mistaked code: :(
You need to put you echo inside second loop.

Code: Select all

 
 for ( $i = 0; $i <= 365; $i++ ) {
    
    for ( $j = 0; $j <= 5; $j++) {
        echo $i . " - ";
       echo $j . "<br />";
    }
 }
 

Re: how to begin an infinite loop, incremented daily

Posted: Mon Jan 05, 2009 11:49 am
by Mark Baker

Code: Select all

 
for ( $i = 0; $i <= 365; $i++ ) {
   echo $i . " - ";
   echo ($i % 6) . "<br />";
}
 

Re: how to begin an infinite loop, incremented daily

Posted: Mon Jan 05, 2009 11:49 am
by VladSun

Code: Select all

for ( $i = 0; $i <= 365; $i++ ) {
   echo $i . " - ".($i%(5+1));
}
:) Mark beat me :)

Re: how to begin an infinite loop, incremented daily

Posted: Mon Jan 05, 2009 12:21 pm
by zachalbert
Awesome! Those last two worked brilliantly. Thanks for the help.