Rotate content by month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Rotate content by month

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Data attained from date() maybe?
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd go with a switch() myself.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thank you - I'll go look up how to properly use switch statements!
sirholiday
Forum Newbie
Posts: 7
Joined: Wed Aug 15, 2007 8:41 am

Post 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;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Time could be saved in sirholiday's code using a modulus operator and removing the while loop altogether.
sirholiday
Forum Newbie
Posts: 7
Joined: Wed Aug 15, 2007 8:41 am

Post by sirholiday »

could you give an example?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$rotation = intval(date('n')) % count($choice);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
sirholiday
Forum Newbie
Posts: 7
Joined: Wed Aug 15, 2007 8:41 am

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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++;
}
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
Eww. And W3C too... Luckily, they don't control PHP standards. :P
sirholiday
Forum Newbie
Posts: 7
Joined: Wed Aug 15, 2007 8:41 am

Post by sirholiday »

Code: Select all

while ($i < $month) {
    if ($rotation >= $numRo) {
        $rotation = 0;
    } else {
        $rotation++;
    }

    $i++;
}
now that is hard to read!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply