PHP - 在元素之间平均订购幻灯片7天(可扩展)

时间:2013-12-03 19:46:27

标签: php

目前我在7天内显示4张幻灯片,如下面的代码所示。我是这样做的,所以每个项目在滑动一,二,三等等方面得到公平的转变。任何人都可以看到更好的方法来做到这一点吗?目前,如果我想添加一张新幻灯片,我必须重新安排每个不理想的变量的顺序。

<?php
$varOne = "<li>varOne item</li>";
$varTwo = "<li>varTwo item</li>";
$varThree = "<li>varThree item</li>";
$varFour = "<li>varFour item</li>";

date_default_timezone_set("Europe/London");
$day = date("D");

if($day == "Mon"){
    $items = compact('varOne','varTwo','varThree','varFour');

} elseif($day == "Tue"){
    $items = compact('varFour','varOne','varTwo','varThree');

} elseif($day == "Wed"){
    $items = compact('varThree','varFour','varOne','varTwo');

} elseif($day == "Thur"){
    $items = compact('varTwo','varThree','varFour','varOne');

} elseif($day == "Fri"){
    $items = compact('varOne','varTwo','varThree','varFour');

} elseif($day == "Sat"){
    $items = compact('varFour','varOne','varTwo','varThree');

} elseif($day == "Sun"){
    $items = compact('varThree','varFour','varOne','varTwo');

}

echo "<ul>";
foreach($items as $item){
    echo $item;
}
echo "</ul>";
?>

2 个答案:

答案 0 :(得分:3)

只需填写$items数组即可完成:

$items = array(
    "<li>varOne item</li>",
    "<li>varTwo item</li>",
    "<li>varThree item</li>",
    "<li>varFour item</li>",
);

date_default_timezone_set("Europe/London");
$start = date('N') - 1; // not quite right but working    

$infinite = new InfiniteIterator(new ArrayIterator($items));
$limit = new LimitIterator($infinite, $start, count($items));

echo "<ul>";
foreach($limit as $item){
    echo $item;
}
echo "</ul>";

<强>解释

它适用于InfiniteIteratorLimitIterator的组合。

  

InfiniteIterator允许一个无限迭代迭代器,而不必在到达终点时手动倒回迭代器。

LimitIterator

  

LimitIterator类允许迭代Iterator中的有限子项。

只需查看并尝试我发布的页面上的示例。 SPL(标准PHP库)中有更多的迭代器。他们非常有帮助。

SPL Iterators

date('N')返回整数1 (for Monday) through 7 (for Sunday)。所以我只减了一个,因为LimitIterator $offset$start)基于零。我对not quite right but working发表了评论,因为你使用了另一个轮换。 (周二应该以varFour而不是varTwo开头)只是尝试更改$start变量。我把它作为挑战留给你。 ;)

顺便说一下,看看SPL提供的其他东西是值得的。您可以使用非常酷的数据结构,例外和接口等,而不必重新发明轮子。

Standard PHP Library (SPL)

答案 1 :(得分:0)

如果您知道,例如,7是允许的最大幻灯片数量

$varOne = "<li>varOne item</li>";
$varTwo = "<li>varTwo item</li>";
$varThree = "<li>varThree item</li>";
$varFour = "<li>varFour item</li>";
$varFive = ""; // populate if and when required
$varSix = ""; // populate if and when required
$varSeven = ""; // populate if and when required

date_default_timezone_set("Europe/London");
$day = date("D");

if($day == "Mon"){
    $items = compact('varOne','varTwo','varThree','varFour','varFive','varSix','varSeven');

} elseif($day == "Tue"){
    $items = compact('varSeven','varFour','varOne','varTwo','varThree','varFive','varSix');

} elseif($day == "Wed"){
    $items = compact('varSix','varSeven','varThree','varFour','varOne','varTwo','varFive');

} elseif($day == "Thur"){
    $items = compact('varFive','varSix','varSeven','varTwo','varThree','varFour','varOne');

} elseif($day == "Fri"){
    $items = compact('varOne','varTwo','varThree','varFour','varFive','varSix','varSeven');

} elseif($day == "Sat"){
    $items = compact('varFour','varOne','varFive','varSix','varSeven','varTwo','varThree');

} elseif($day == "Sun"){
    $items = compact('varThree','varFour','varFive','varSix','varSeven','varOne','varTwo');

}

echo "<ul>";
foreach($items as $item){
    if($item !=""){
        echo $item;
    }
}
echo "</ul>";