Jump to content

Archived

This topic is now archived and is closed to further replies.

Twentyoneth

if/while

Recommended Posts

I'm wanting to show the 5 most current upcoming events, the files will be labled 20060401.php, the year month and day in date(Ymd), and it will add a number to date, each time ect until it comes accross a date that is a file....the problem is, nothing shows up, I have echo'd out $file, and the other variables, and they all seem to work, but when it gets to my while and if statements it all stops....


[code]$dir = 'upcoming/';
$date = date('Ymd');
$number = 0;
$file= ($dir . $date . ".php");

while($number > 6) {
      if(is_file($file)) {
         if($number >= 4) {
            echo "<a href='index.php?file=" . $date . ".php'>" . $date . "</a><br>";
            $number++;
            $date++;
           } else {
            echo "<a href='index.php?file=" . $date . ".php'>" . $date . "</a>";
            $number++;
            $date++;
           }
        } else {
         $date++;
        }
     }

?>[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=363778:date=Apr 11 2006, 03:10 PM:name=Twentyoneth)--][div class=\'quotetop\']QUOTE(Twentyoneth @ Apr 11 2006, 03:10 PM) [snapback]363778[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I'm wanting to show the 5 most current upcoming events, the files will be labled 20060401.php, the year month and day in date(Ymd), and it will add a number to date, each time ect until it comes accross a date that is a file....the problem is, nothing shows up, I have echo'd out $file, and the other variables, and they all seem to work, but when it gets to my while and if statements it all stops....
[code]$dir = 'upcoming/';
$date = date('Ymd');
$number = 0;
$file= ($dir . $date . ".php");

while($number > 6) {
      if(is_file($file)) {
         if($number >= 4) {
            echo "<a href='index.php?file=" . $date . ".php'>" . $date . "</a><br>";
            $number++;
            $date++;
           } else {
            echo "<a href='index.php?file=" . $date . ".php'>" . $date . "</a>";
            $number++;
            $date++;
           }
        } else {
         $date++;
        }
     }

?>[/code]
[/quote]

Hi.
I have just scanned through the code quickly, and from it, it seems the while loop is never performed.
You are setting $number=0 and then you have while condition to be $number>6 which is never since $number=0 therefore $number<6. It should either be $number <6 or if you want at least one iteration, you should use do...while loop.

Share this post


Link to post
Share on other sites
Adding 1 to a number containing the string that contains YYYYMMDD will not work correctly if your starting date is within n days of the end of the month, where n is less than the number of days you're looking for. You should be getting the UNIX timestamp for the starting date and adding 86400 for each day, then converting it back to the YYYYMMMDD format. 86400 is the number of seconds in a day. A UNIX timestamp is the number of seconds that have elapsed since the base time of Jan 1, 1970.

Try this:
[code]<?php
$dir = 'upcoming/';
$tmp = array();
$num = 0;
$date = time();
while ($num < 6) {
      $file = date('Ymd',$date) . '.php');
      if(is_file($dir . $file)) {
            $tmp[] = '<a href="index.php?file=' . $file . '.php">' . date('Ymd',$date) . '</a>';
            $num++;
      }
      $date+= 86400;
}
echo implode("<br>\n",$tmp);
?>[/code]

This code has not been tested.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.