Jump to content

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]
Link to comment
https://forums.phpfreaks.com/topic/7136-ifwhile/
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.

Link to comment
https://forums.phpfreaks.com/topic/7136-ifwhile/#findComment-26202
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
Link to comment
https://forums.phpfreaks.com/topic/7136-ifwhile/#findComment-26223
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.