Rotate content by month
Moderator: General Moderators
Rotate content by month
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!
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!
Ok so would it be correct to do something like this
It works but I'd like to check if there's a better way or if there are any flaws with this approach.
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';
}
?>- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
sirholiday
- Forum Newbie
- Posts: 7
- Joined: Wed Aug 15, 2007 8:41 am
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;
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$rotation = intval(date('n')) % count($choice);- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
sirholiday
- Forum Newbie
- Posts: 7
- Joined: Wed Aug 15, 2007 8:41 am
here's a new updated version, thanks to feyd
superdezign, as for how I indent, well thats what I think is easy to read... lets see
is this what you would do?
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
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++;
}
?>- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
At least, that's what I'd do a few days ago. I just started complying with PHP standards, recently.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++; } ?>
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.sirholiday wrote: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
-
sirholiday
- Forum Newbie
- Posts: 7
- Joined: Wed Aug 15, 2007 8:41 am
Code: Select all
while ($i < $month) {
if ($rotation >= $numRo) {
$rotation = 0;
} else {
$rotation++;
}
$i++;
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm