Page 1 of 1
Rotate content by month
Posted: Tue Aug 14, 2007 11:03 am
by Noobie
Hi everyone
What's the best way to organise rotating content (just the include call) by month?
There'll be 3 different sets of content to swap over - one a month, in a set order. Unfortunately I don't think I can just base it on a 30 day period either as they need it to be more precise than that.
Any suggestions welcomed!
Posted: Tue Aug 14, 2007 11:22 am
by feyd
Data attained from
date() maybe?
Posted: Tue Aug 14, 2007 12:44 pm
by Noobie
Ok so would it be correct to do something like this
Code: Select all
<?php
$month = date("F");
if ($month == "January" || $month == "April" || $month == "July" || $month == "October") {
include 'content1.php';
}
if ($month == "February" || $month == "May" || $month == "August" || $month == "November") {
include 'content2.php';
}
if ($month == "March" || $month == "June" || $month == "September" || $month == "December") {
include 'content3.php';
}
?>
It works but I'd like to check if there's a better way or if there are any flaws with this approach.
Posted: Tue Aug 14, 2007 2:54 pm
by superdezign
You could get the month as a number instead of a string.
As for actually improving on the code... Turn those ifs into else-ifs and elses.
Posted: Tue Aug 14, 2007 9:47 pm
by feyd
I'd go with a switch() myself.
Posted: Wed Aug 15, 2007 7:10 am
by Noobie
Thank you - I'll go look up how to properly use switch statements!
Posted: Wed Aug 15, 2007 8:47 am
by sirholiday
I made up this thing in a jif... but it is much more dynamic than what you had.... have fun!
Code: Select all
<?php
//set choices (add as many as needed)
$choice[0] = "content for rotatoin 1";
$choice[1] = "content for rotatoin 2";
$choice[2] = "content for rotatoin 3";
//get current month number
$month = date("n");
//count number of choices in array
$num_ro = count($choice)-1;
//set some default values
$i = 1;
$rotation = 0;
//find choice
while($i<$month)
{
if($rotation>=$num_ro)
{
$rotation = 0;
}
else
{
$rotation++;
}
$i++;
}
//set final choice
$finel_choice = $choice[$rotation];
//echo final choice or whatever
echo $finel_choice;
?>
Posted: Wed Aug 15, 2007 8:50 am
by feyd
Time could be saved in sirholiday's code using a modulus operator and removing the while loop altogether.
Posted: Wed Aug 15, 2007 8:53 am
by sirholiday
could you give an example?
Posted: Wed Aug 15, 2007 8:55 am
by feyd
Code: Select all
$rotation = intval(date('n')) % count($choice);
Posted: Wed Aug 15, 2007 9:08 am
by superdezign
BTW sirholiday, your bracket indentation is strange and makes it difficult to follow what brackets apply to which statements. Is that a habit?
Posted: Wed Aug 15, 2007 9:20 am
by sirholiday
here's a new updated version, thanks to feyd
Code: Select all
<?php
//set choices (add as many as needed)
$choice[0] = "content for rotatoin 1";
$choice[1] = "content for rotatoin 2";
$choice[2] = "content for rotatoin 3";
//get current month number
$month = date("n");
//count number of choices in array
$num_ro = count($choice);
//find choice
$rotation = intval($month-1) % $num_ro;
//set final choice
$finel_choice = $choice[$rotation];
//echo final choice or whatever
echo $finel_choice;
?>
superdezign, as for how I indent, well thats what I think is easy to read... lets see
is this what you would do?
Code: Select all
<?php
//find choice
while($i<$month)
{
if($rotation>=$num_ro)
{
$rotation = 0;
}
else
{
$rotation++;
}
$i++;
}
?>
I find that more confusing... and I'm not the only one who indents my way, have a look
http://www.w3schools.com/php/php_functions.asp
Posted: Wed Aug 15, 2007 9:49 am
by superdezign
sirholiday wrote:is this what you would do?
Code: Select all
<?php
//find choice
while($i<$month)
{
if($rotation>=$num_ro)
{
$rotation = 0;
}
else
{
$rotation++;
}
$i++;
}
?>
At least, that's what I'd do a few days ago. I just started complying with PHP standards, recently.
Code: Select all
while ($i < $month) {
if ($rotation >= $numRo) {
$rotation = 0;
} else {
$rotation++;
}
$i++;
}
Eww. And W3C too... Luckily, they don't control PHP standards.

Posted: Wed Aug 15, 2007 5:44 pm
by sirholiday
Code: Select all
while ($i < $month) {
if ($rotation >= $numRo) {
$rotation = 0;
} else {
$rotation++;
}
$i++;
}
now that is hard to read!
Posted: Wed Aug 15, 2007 6:10 pm
by superdezign
sirholiday wrote:now that is hard to read!
That's what I thought at first too, but after I actually started typing it myself, it became easier to follow.