Jump to content


Photo

Rotate Dynamic Content Every Day


  • Please log in to reply
10 replies to this topic

#1 mumford

mumford
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 12:23 PM

Hi

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

<?
/* Directory Structure
/index1.htm
/index2.htm

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

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

?>

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

#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 14 September 2006 - 12:50 PM

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.

#3 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 01:05 PM

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.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#4 mumford

mumford
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 01:07 PM

Thanks fo rthe quick replies, like I sadi I know very lttle PHP (at the moment!)

So I would have to create a database where all of my text divs would sit, then call them from the homepage?

#5 mumford

mumford
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 02:14 PM

Can anyone else shed some more light on how I go about setting this up to switch content on a daily basis, do I really have to set up a database?....... there will only be about 5 alternative text blocks

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 September 2006 - 03:22 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#7 mumford

mumford
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 03:45 PM

sounds exactly what i need, but I dont even know where to start to get that working, your help or guidance would be much appreciated.

thanks

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 September 2006 - 04:04 PM

<?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);
?>

That'll need files news_0.php, news_1.php ..... news_4.php. Good luck.
Legend has it that reading the manual never killed anyone.
My site

#9 mumford

mumford
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 06:10 PM

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

<?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);
?>

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

#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 September 2006 - 07:49 PM

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:

<?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);
?>

You can add file names to the array and the rest of the code can remain as written.
Legend has it that reading the manual never killed anyone.
My site

#11 mumford

mumford
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 09:27 AM

great stuff! - thanks again for your help




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users