homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Script to Rotate "Tips"
rotates every "X" days
old_expat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3990114 posted 4:57 am on Sep 16, 2009 (gmt 0)

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?

 

adb64

10+ Year Member



 
Msg#: 3990114 posted 6:22 am on Sep 16, 2009 (gmt 0)

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

Lexur

5+ Year Member



 
Msg#: 3990114 posted 6:47 am on Sep 16, 2009 (gmt 0)

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.

old_expat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3990114 posted 2:31 am on Sep 17, 2009 (gmt 0)

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. :)

old_expat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3990114 posted 2:41 am on Sep 17, 2009 (gmt 0)

@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.

Lexur

5+ Year Member



 
Msg#: 3990114 posted 5:45 am on Sep 17, 2009 (gmt 0)

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.

adb64

10+ Year Member



 
Msg#: 3990114 posted 11:11 am on Sep 17, 2009 (gmt 0)

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

old_expat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3990114 posted 6:30 am on Oct 10, 2009 (gmt 0)

This is probably a dumb question, but can a PHP script like this display a given file on a given day of the week?

coopster

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



 
Msg#: 3990114 posted 11:45 pm on Oct 29, 2009 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved