photodow Posted March 3, 2007 Share Posted March 3, 2007 Alright, I am trying to include a file every year from my array. My only problem is, is that I have only three files to cycle through. Here is my script: -------------------- <?php $year = date('y'); $file_index = $year - 5; $files = array("classbaby.html","classhigh.html","class456.html"); $week_file = $files[$file_index]; include $week_file; ?> -------------------- Now this year should be displaying the file called "class456.html", and I'm using the - 5 to get that. Well this is all working wonderfully until i replace - 5 with - 4 to see what happens on my fourth year of this script. All I get is an error saying the file doesn't exist. Do any of you know how I can loop it around to where ever four years it starts over? I assume it has to do with the loop command, but I haven't been able to figure it out yet. Thanks for any help you can give! Link to comment https://forums.phpfreaks.com/topic/41010-solved-problem-with-array/ Share on other sites More sharing options...
ted_chou12 Posted March 3, 2007 Share Posted March 3, 2007 if you try subtract 4 (-4) then it would result in this: this year 07 - 4 = 3 However, there are only three strings in your array, put $week_file = $files[3]; would result it to read the 4th string. therefore it gives you nothing. Ted Link to comment https://forums.phpfreaks.com/topic/41010-solved-problem-with-array/#findComment-198586 Share on other sites More sharing options...
photodow Posted March 3, 2007 Author Share Posted March 3, 2007 Yes I think I understand that part of it. But what I'm trying to do is get it to where on the fourth year of this script running it starts over to where it displays "classbaby.html" and continues with the rest of the array. Here is sort of what I want it to do: Display Year one: classbaby.html Year two: classhigh.html year three: class456.html year four: classbaby.html year five: classhigh.html year six: class456.html And so on, and so forth. Does that make since? You see I think I can write out those three about 100 times, and it would do what I want it to do, but when trying to re-edit the script or going in to add some other file that would be a pain in the back. Link to comment https://forums.phpfreaks.com/topic/41010-solved-problem-with-array/#findComment-198593 Share on other sites More sharing options...
ted_chou12 Posted March 3, 2007 Share Posted March 3, 2007 try modulas: <?php $year = date('y'); $files = array("classbaby.html","classhigh.html","class456.html"); if ($year % 3 == 0) {include ($files[2]);} elseif ($year % 2 == 0) {include ($files[1]);} else {include ($files[0]);} ?> Ted Link to comment https://forums.phpfreaks.com/topic/41010-solved-problem-with-array/#findComment-198599 Share on other sites More sharing options...
photodow Posted March 3, 2007 Author Share Posted March 3, 2007 Thanks that worked perfectly! Link to comment https://forums.phpfreaks.com/topic/41010-solved-problem-with-array/#findComment-198609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.