Problems with while loop date

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
zip_000
Forum Newbie
Posts: 3
Joined: Mon Aug 18, 2008 9:53 am

Problems with while loop date

Post by zip_000 »

Hello,

I'm trying to write a while loop that will test first whether a file exists for previous months, and if so display a link to it, but for some reason I get an extra link...It seems to be going through the loop an extra time. Can someone tell me what I'm doing wrong?

Code: Select all

 
                        $i = -1;
                        $date = date("F", strtotime("$i month"));
                        
                        $file = $date.".php";
                        while (file_exists($file) == true){
                        
                        $date = date("F", strtotime("$i month"));
                        $file = $date.".php";
                        echo "<h2><a href=\"".$date.".php\">$date</a></h2>";
                        $i = $i-1;
                        }
                       
Thanks!
etnastyles
Forum Newbie
Posts: 3
Joined: Mon Jul 07, 2008 4:26 am

Re: Problems with while loop date

Post by etnastyles »

$i = -1;
$date = date("F", strtotime("$i month"));
if (file_exists($file))
{
while (file_exists($file))
{
$date = date("F", strtotime("$i month"));
$file2 = $date.".php";
echo "<h2><a href=\"".$date2.".php\">$date</a></h2>";
$i = $i-1;
}
}
else
{
/* make file */
}
zip_000
Forum Newbie
Posts: 3
Joined: Mon Aug 18, 2008 9:53 am

Re: Problems with while loop date

Post by zip_000 »

Thanks for the help, but the code posted doesn't seem to do what I want either.

I tried just that code, and with that I end up with a never ending list of months.

I also tried taking putting the while loop that I have into the if statement that you used, but I still just get the same list that I got, i.e. I have files for March through July, but the code lists February as well. If I add a February file, then the list will show January, etc.

Any other suggestions about what I'm doing wrong?

Thanks!
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Problems with while loop date

Post by ghurtado »

You are doing it backwards within the loop, you check whether the previous file exists, then you change it to the next filename and show the link. This will make it so that the last one is always a bad link.

Make the "echo" line the first one in your loop.
zip_000
Forum Newbie
Posts: 3
Joined: Mon Aug 18, 2008 9:53 am

Re: Problems with while loop date

Post by zip_000 »

Thanks for the help, I've got it. Here's the code that worked finally:

Code: Select all

                        $i = -1;
                        $date = date("F", strtotime("$i month"));
                        $file = $date.".php";
                        while (file_exists($file))
                            {
                                $date = date("F", strtotime("$i month"));
                                $file = $date.".php";
                                if (file_exists($file))
                                    {
                                        echo "<h2><a href=\"".$date.".php\">$date</a></h2>";                
                                    }
                                $i = $i-1;
                            }
I suspect this isn't the most elegant way of making this work, but it works.

Again thanks for the help.
Post Reply