I am looking to have a specific cfm page be included on my home page (index.php) according to a specific date.
i.e.: 01/01/2013 - 05/15/2013 display wet start.cfm
05/16/2013 - 05/31/2013 display first splash.cfm
06/01/2013 - 06/15/2013 display splashback.cfm
and so on until the last event...
06/16/2013 - 12/31/2013 display season over.cfm
My include pages are listed below, but I can not figure out how to get the correct if-else statement.
<center><b>Our Next Event:</b></center>
<?php include '../includes/events/wet start.cfm'; ?>
<?php include '../includes/events/first splash.cfm'; ?>
<?php include 'includes/events/splashback.cfm'; ?>
<?php include '../includes/events/mardi gras.cfm'; ?>
<?php include '../includes/events/fiesta.cfm'; ?>
<?php include '../includes/events/steak feed.cfm'; ?>
<?php include '../includes/events/luau.cfm'; ?>
<?php include '../includes/events/tiki.cfm'; ?>
<?php include '../includes/events/toga.cfm'; ?>
<?php include '../includes/events/lumberyard.cfm'; ?>
<?php include '../includes/events/royal court.cfm'; ?>
<?php include '../includes/events/bullards bar.cfm'; ?>
<?php include '../includes/events/dance party.cfm'; ?>
<?php include '../includes/events/houseboat.cfm'; ?>
<?php include '../includes/events/season over.cfm'; ?>
I would do something like this:
<?php
// function that will return the file path for the current event as a string
function getCurrentEventFilepath() {
// list all the events, with start date => file name
// make sure they are sorted by date, or do sorting before you proceed
$eventList = array(
'2013-01-01' => 'start',
'2013-05-16' => 'splash',
'2013-06-01' => 'splashback',
'2013-06-15' => 'mardi_gras',
'2013-06-30' => 'fiesta'
);
// prepare some variables
$tmpFile = '';
$today = new DateTime("now");
// loop through the event list
foreach ($eventList as $dateString => $file) {
// construct a date based on the dateString
$date = new DateTime($dateString);
// if the date is later then today
if ($today < $date) {
// return the path based on filename from the previos iteration
return '../includes/events/'.$tmpFile.'.cfm';
} else {
// store the filaname in our temp variable
$tmpFile = $file;
}
}
// if still here, it should be the last event in the list we want
return '../includes/events/'.$tmpFile.'.cfm';
}
// fetch the current event file path, and do an include
include getCurrentEventFilepath();
?>
I put plenty of comments in the code, but feel free to ask if you want further explaination