Welcome to WebmasterWorld Guest from 54.147.220.66

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Script to Rotate "Tips"

rotates every "X" days

   
4:57 am on Sep 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script that a nice fellow wrote for me a couple of years ago. It rotates "tips" and various other stuff depending on where I use it.

The only problem with a current requirement is that the script rotates on every page load. I would like to be able to rotate like every "X" days.

The script I have now:


$banners_array = array
(
'tip1.php',
'tip2.php',
'tip3.php',
'tip4.php',
'tip5.php'
);
$i = array_rand($banners_array, 1);
$banner = $banners_array[$i];
include_once $banner;

How could this code be rewritten?

6:22 am on Sep 16, 2009 (gmt 0)

10+ Year Member



Hi old_expat,

Base the index on the current day number


$RotateEveryXDays = 3; // Every 3 days another banner
$banners_array = array
(
'tip1.php',
'tip2.php',
'tip3.php',
'tip4.php',
'tip5.php'
);
$DayNr = time() / 86400; // Get day number (86400 = # seconds per day)
$BannerNr = $DayNr / $RotateEveryXDays; // WHich banner to use for this day
$i = $BannerNr % count($banners_array); // But limit to number of available banners
$banner = $banners_array[$i];
include_once $banner;

Regards,
Arjan

6:47 am on Sep 16, 2009 (gmt 0)

5+ Year Member



The simplest way:

<?php include "files/".date('m').date('d').".txt" ; ?>

and then fill the folder files/ with text files called 0101.txt till 3112.txt to have a tip for every day of the year.

2:31 am on Sep 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello adb64,

Thanks so much.

I'm kind of a dummy, so may I assume that the only value I need to change is the '3' in order to set the rotation?

$RotateEveryXDays = 3; // Every 3 days another banner
$banners_array = array
(
'tip1.php',
'tip2.php',
'tip3.php',
'tip4.php',
'tip5.php'
);
$DayNr = time() / 86400; // Get day number (86400 = # seconds per day)
$BannerNr = $DayNr / $RotateEveryXDays; // WHich banner to use for this day
$i = $BannerNr % count($banners_array); // But limit to number of available banners
$banner = $banners_array[$i];
include_once $banner;

I will work my way through this one line at a time and try to learn a little bit more about PHP .. onward and upward. :)

2:41 am on Sep 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@Lerxur - I'm not sure I understand how this code works. What if I want to rotate only 2 files? (in one case I do want only 2) And I want each file to display for 'X' days.

One thing I am trying is alternating a few affiliate text links vs a block of Adsense code.

In another case I want to alternate several different Adsense blocks .. partly for testing, partly for ad blindness.

5:45 am on Sep 17, 2009 (gmt 0)

5+ Year Member



You can reduce to this:

<?php include "files/".date('d').".txt" ; ?>

and fill the files/ folder with files from 01.txt to 31.txt and use odd files to Adsense and even to links (or whatever you want). Anyway yo must write/copy and rename the 31 files. The easy way is using the copy command and the old batch files.

11:11 am on Sep 17, 2009 (gmt 0)

10+ Year Member



old_expat,

The 3 indeed defines the rotation frequency in days, so every 3 days the banner is changed. If you want to change once a week, change it to 7.

Regards,
Arjan

6:30 am on Oct 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is probably a dumb question, but can a PHP script like this display a given file on a given day of the week?
11:45 pm on Oct 29, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes. The date() function can tell you which day of the week it is. You can pull the given file for the given day of the week based on the switch. Off the top of my head, I believe it is "w" but you'll have to check the manual page to be certain.