Page 1 of 1

Problems with while loop date

Posted: Mon Aug 18, 2008 10:01 am
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!

Re: Problems with while loop date

Posted: Mon Aug 18, 2008 10:23 am
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 */
}

Re: Problems with while loop date

Posted: Mon Aug 18, 2008 12:22 pm
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!

Re: Problems with while loop date

Posted: Mon Aug 18, 2008 12:34 pm
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.

Re: Problems with while loop date

Posted: Mon Aug 18, 2008 12:46 pm
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.