how to begin an infinite loop, incremented daily

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

how to begin an infinite loop, incremented daily

Post 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.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Re: how to begin an infinite loop, incremented daily

Post 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 />";
    }
 }
 
Last edited by xpgeek on Tue Jan 06, 2009 3:57 am, edited 1 time in total.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: how to begin an infinite loop, incremented daily

Post by Mark Baker »

Code: Select all

 
for ( $i = 0; $i <= 365; $i++ ) {
   echo $i . " - ";
   echo ($i % 6) . "<br />";
}
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: how to begin an infinite loop, incremented daily

Post by VladSun »

Code: Select all

for ( $i = 0; $i <= 365; $i++ ) {
   echo $i . " - ".($i%(5+1));
}
:) Mark beat me :)
There are 10 types of people in this world, those who understand binary and those who don't
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: how to begin an infinite loop, incremented daily

Post by zachalbert »

Awesome! Those last two worked brilliantly. Thanks for the help.
Post Reply