mattblank Posted December 10, 2007 Share Posted December 10, 2007 Hi, I'm not a wizz at PHP by any stretch of the imagination so I could really do with some help! I want to put an <? include ("file.php"); ?> on a page...simple. However let's say I have 70 pages, and I want to display a different one each week (every 7 days). Is there a script I can put in a page that would load a different <? include ("file.php"); ?> every seven days on a page? So for instance it would be: Week 1: <? include ("file1.php"); ?> Week 2: <? include ("file2.php"); ?> Week 3: <? include ("file3.php"); ?> etc... I would prefer it to be sequential like above, but if it's a lot easier to make it generate a random file out of the 70 then so be it. As long as it is only changed every 7 days i.e. changes every Monday. I hope that makes sense and someone can help! Many thanks in advance, Matt Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 10, 2007 Share Posted December 10, 2007 You can do <?php include("file".date("W").".php"); ?> but as per my knowlegde I think there are 52 weeks in a year Quote Link to comment Share on other sites More sharing options...
mattblank Posted December 10, 2007 Author Share Posted December 10, 2007 Thanks for the quick response! So where would I store all my includes so that it knows where to pull it from? Or if I just put file1.php and file2.php etc... in the same folder will it know just to pick it from there?? Thanks! Matt Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 10, 2007 Share Posted December 10, 2007 currently we are running in the 50 week of the year so it will file50.php once we cross next week it will include file51.php and onwards however you can write a better include system based on a give date so that you give a date from that date until 7 days it will include file1.php... Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 10, 2007 Share Posted December 10, 2007 here I wrote you something <?php function dynamicinclude($filename,$strDate,$intMax) { $intTime = strtotime($strDate); $intToday = time(); $intDifference = $intToday-$intTime; $intWeek = ceil($intDifference/(86400*7)); $strFilename = $filename.($intWeek%$intMax).".php"; include($strFilename); } dynamicinclude("file","2007-12-10",70); ?> Param1 = "file" is the string so basically it will be file1.php and so on you can change it to something else Param2 = start date of the include Param3 = max number of include file so it will rotate hope its helpful Quote Link to comment Share on other sites More sharing options...
mattblank Posted December 10, 2007 Author Share Posted December 10, 2007 Hi rajivgonsalves, Thank you so much for the time you're giving me...it's really appreciated! Just to confirm. All I need to do it have all my files in one folder and it will pick them from there? And due to having 52 weeks in a year, does that mean I can only have 52 files? Or will it just keep on going from the start date I put in until file 70 (for example). Lastly (I promise!) once it gets to file 70, will it then repeat? Many thanks!!!! Matt Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 10, 2007 Share Posted December 10, 2007 Yes you just give a start date parameter two, if you have more files than 70 change the last parameter, so say you got 100 files put 100 there it will rotate, if your putting it in some folder just change the include statement from include($strFilename); to include("includes/".$strFilename); if all files are residing in the include folder. hope it helpful Quote Link to comment Share on other sites More sharing options...
mattblank Posted December 10, 2007 Author Share Posted December 10, 2007 Brilliant!! Many thanks for your help! Matt Quote Link to comment Share on other sites More sharing options...
mattblank Posted December 10, 2007 Author Share Posted December 10, 2007 Hi rajivgonsalves, Just having a few problems I'm using the below code: <? function dynamicinclude($filename,$strDate,$intMax) { $intTime = strtotime($strDate); $intToday = time(); $intDifference = $intToday-$intTime; $intWeek = ceil($intDifference/(86400*7)); $strFilename = $filename.($intWeek%$intMax).".php"; include("../includes/songs/".$strFilename); } dynamicinclude("song","2007-10-15",3); ?> So I've got three files in my includes/songs/ folder called song1.php, song2.php and song3.php. To make sure it works I keep setting the date back in time...but it seems to only ever display two of the files and then every three weeks it shows nothing. Am I doing something wrong?? Many thanks, Matt Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 11, 2007 Share Posted December 11, 2007 Your not doing anything wrong there was a bug in the code new code below <?php function dynamicinclude($filename,$strDate,$intMax) { $intTime = strtotime($strDate); $intToday = time(); $intDifference = $intToday-$intTime; $intWeek = floor($intDifference/(86400*7)); $intCounter = $intWeek%$intMax+1; $strFilename = $filename.$intCounter.".php"; include("../includes/songs/".$strFilename); } dynamicinclude("song","2007-11-20",3); ?> lemme know if you face any problems Quote Link to comment Share on other sites More sharing options...
mattblank Posted December 11, 2007 Author Share Posted December 11, 2007 Thanks rajivgonsalves! That seems to have done the trick. Many thanks for all your help, really grateful! Matt Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 11, 2007 Share Posted December 11, 2007 Always glad to be of help! Quote Link to comment Share on other sites More sharing options...
mattblank Posted March 26, 2017 Author Share Posted March 26, 2017 Hi! I'm following this up (10 years later!!). I'm still using the above code that rajivgonsalves provided. I would now like to include this code twice on the same page, but I get an error saying "Cannot redeclare dynamicinclude() (previously declared". Can anyone help me please? Thanks! Matt Quote Link to comment 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.