Jump to content


Photo

if/while


  • Please log in to reply
2 replies to this topic

#1 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 11 April 2006 - 08:10 PM

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....


$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++;
        }
     }

?>


#2 mystxx

mystxx
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 April 2006 - 01:20 PM

[!--quoteo(post=363778:date=Apr 11 2006, 03:10 PM:name=Twentyoneth)--][div class=\'quotetop\']QUOTE(Twentyoneth @ Apr 11 2006, 03:10 PM) View Post[/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....
$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++;
        }
     }

?>
[/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.



#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 02:26 PM

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:
<?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);
?>

This code has not been tested.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users