你能帮我解决在foreach中嵌套的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>
EDIT第一段代码xml2array.php工作正常,第二段代码tv_schedule.php, if(date('l', strtotime($schedule['start'])) == $select){…}是主要问题。对不起,误导了。
所以你说if
语句中的代码(顺便说一下,if
不是循环)不执行?那么很可能是因为它检查的条件为假。
原因可能在循环上面的代码中。此条件错误:
if($select == 'Today' OR 'today') {
$select = $today;
}
应该(可能)是:
if ($select == 'Today' or $select == 'today') {
$select = $today;
}
同样适用于该代码段中的其他类似条件。此错误导致$select
的值与预期值不同。这是你可以很容易地发现使用一些穷人的调试,即,通过var_dump
值你比较你的if
条件。