0

我希望根据特定日期在我的主页 (index.php) 中包含特定的 cfm 页面。

即: 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
和依此类推,直到最后一个活动...
06/16/2013 - 12/31/2013 显示季节 over.cfm

我的包含页面如下所列,但我不知道如何获得正确的 if-else 语句。

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

4

1 回答 1

0

我会做这样的事情:

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

我在代码中添加了大量注释,但如果您需要进一步解释,请随时询问

于 2013-06-15T14:48:50.220 回答