Jump to content

[SOLVED] Different Include Files On Rotation?


mattblank

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 9 years later...

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

Link to comment
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.