如果声明多条件检查

时间:2014-07-10 18:57:29

标签: php arrays

你能帮我解决在ifach中嵌套的if循环中覆盖$ tpl变量(仅保留最后的foreach迭代数据)的问题。如果我删除嵌套的if循环(只保留foreach循环),整个电视节目列在页面上。我的目标是通过选择树输入类型之一来显示电视节目="提交"。代码分为两个文件:xml2array.php和tv_schedule.php。提前谢谢。

以下是tv_schedule.php

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <form id="select" action="" method="get" >
            <input type="submit" name="program" value="today" />
            <input type="submit" name="program" value="tomorrow" />
            <input type="submit" name="program" value="afterTomorrow" />
        </form>

        <table id="program" class="table tablesorter">
            <thead>
                <tr>
                    <th>beginning</th>
                    <th>end</th>
                    <th>programme</th>
                </tr>
            </thead>
        <tbody>
        <?php
        include_once('xml2array.php');
        $today = date('l');
        $tomorrow = date('l', strtotime("+1 day"));
        $afterTomorrow = date('l', strtotime("+2 day"));

        if(isset($_GET['program'])) {
            $select=$_GET['program'];
            if($select == 'Today' OR 'today') {
                $select = $today;
            }
            if($select == 'Tomorrow' OR 'tomorrow') {
                $select = $tomorrow;
            }
            if($select == 'AfterTomorrow' OR 'afterTomorrow') {
                $select = $afterTomorrow;
            }               
            foreach($programXml['programme'] AS $schedule) {
                //critical if loop
                if(date('l', strtotime($schedule['start'])) == $select) {
                    $tpl = "<tr>";
                    $tpl .= "<td>". date('H:i', strtotime($schedule['start']))."</td>";
                    $tpl .= "<td>". date('H:i', strtotime($schedule['stop']))."</td>";
                    $tpl .= "<td>".$schedule['content']['title'][0]['content']."</td>";
                    $tpl .= "</tr>";
                    echo $tpl;
                }// end if
            } // end foreach
        } //end if(isset($_POST['program']))

        ?>
        </tbody>
    </table>
</body>

编辑第一个代码段xml2array.php工作正常,第二个代码为tv_schedule.php,if(date(&#39; l&#39;,strtotime($ schedule [&#39; start&# 39;]))== $ select){...}是主要问题。对不起误导。

1 个答案:

答案 0 :(得分:0)

所以你说的是if语句中的代码(if不是循环,顺便说一句)不执行?那可能是因为它检查的条件是错误的。

原因可能是在循环上面的代码中。这种情况是错误的:

if($select == 'Today' OR 'today') {
    $select = $today;
}

它应该(可能)是:

if ($select == 'Today' or $select == 'today') {
    $select = $today;
}

该代码中的其他类似条件也是如此。此错误导致$select具有与预期不同的值。这是通过使用一些穷人的调试很容易发现的东西,即var_dump你在if条件下比较的值。