Jump to content

Recommended Posts

Hi

I am VERY new tp php, and found this script on php freaks, it is a randomizer script

[code]<?
/* Directory Structure
/index1.htm
/index2.htm

*/
srand((double)microtime()*1000000);
$num = rand(1,2);

include ('index'.$num.'.htm');

?>[/code]

What I need to happen on my website is 3 blocks of text each in its own div to change every day (5 days to be precise), I got the above to work but as I said it is just a randomizer so each time you refresh it changes.

Also my dynamic content pages will sit in its own folder, and I would like to name the pages depending on what they are, eg, capetown.htm, garden-route.htm

Does that make sense, hope someone can help me!

thanks
Link to comment
https://forums.phpfreaks.com/topic/20722-rotate-dynamic-content-every-day/
Share on other sites

it might be best for the script to maybe check for a time of day?

For example. If it is past midnight, the date is different, and the info hasn't already been changed, then save information in the database. (or even a file, who cares in this situation).

then for that entire day it will show "whatever.htm" and at 12 midnight (or later) whenevery someone opens the page, the first person to open it after midnight, it will set this new information accordingly.

The things you'll need to look into to make this work is file reading/writing and date functions. that should be enough to get that idea done.
or use a counter script from a database field like:
switch ($row['counter']){
case 1:
echo 'stuff #1';
break;
}
and keep doing that for each instance
then if it's the following day, update the database using a timestamp checker.
The sort of thing I'd do is use php to get today's date and determine what day number of the year it is.  Then I'd modulo 5 that and based on the remainder, I'd include day0.php, day1.php etc.  No database needed, no counter needed.
[code]<?php
$doty = date("z"); // day of the year
$doty_remainder = $doty % 5 ; // remainder of day of the year divided by 5
$the_file = "path/to/my/files/news_". $doty_remainder. ".php"; // edit path info to suit
include($the_file);
?>[/code]

That'll need files news_0.php, news_1.php ..... news_4.php. Good luck.
Andy B, that looks like it will be the best response I have received over 2 days, thank you!

I suppose if I need to have other items that rotate on a daily basis I just inser the same code but change the filenames eg

[code]<?php
$doty = date("z"); // day of the year
$doty_remainder = $doty % 5 ; // remainder of day of the year divided by 5
$the_file = "[b]dynamic/offers[/b]_". $doty_remainder. ".php"; // edit path info to suit
include($the_file);
?>[/code]

Also as a matter of interest if I did want those 5 pages to rotate randomly is there a way of adjusting the code you gave or would it be something different entirely?

Thanks again
Answer to the first question is "Yes".

as to the second: to have a randomly selected page (from a lsit), I'd use code like this:

[code]<?php
$pages = array("page1.php", "gardening.php", "fishing.php", "whatever.php"); // as many as you want
$the_file = $pages[rand(0,count($pages)-1)]; // select a random page from the array
include($the_file);
?>[/code]

You can add file names to the array and the rest of the code can remain as written.
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.