Twentyoneth Posted April 11, 2006 Share Posted April 11, 2006 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 Link to comment https://forums.phpfreaks.com/topic/7136-ifwhile/ Share on other sites More sharing options...
mystxx Posted April 12, 2006 Share Posted April 12, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/7136-ifwhile/#findComment-26202 Share on other sites More sharing options...
kenrbnsn Posted April 12, 2006 Share Posted April 12, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7136-ifwhile/#findComment-26223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.