[英]why isn't this php loop working?
I have this code: 我有以下代码:
$thisTime = gmmktime(0, 0, 0);
for($i=0; $i<=95; $i++)
{
$perfTimeNumber = ($i+1);
$perfTimestamp = $thisTime;
$perfTime = date("H:i", $perfTimestamp);
echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';
$thisTime = $thisTime+(15*60);
}
This works fine to generate a select input with options from 01:00 through to 24:45 at 15 minute intervals. 这样可以很好地生成以15分钟为间隔的从01:00到24:45的选项的选择输入。 However, if I change the code and add an if statement I get some odd results... 但是,如果我更改代码并添加if语句,则会得到一些奇怪的结果...
$thisTime = gmmktime(0, 0, 0);
for($i=0; $i<=95; $i++)
{
$perfTimeNumber = ($i+1);
$perfTimestamp = $thisTime;
$perfTime = date("H:i", $perfTimestamp);
if ($perfTime == '19:30') {
$sel = "selected";
}
echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';
$thisTime = $thisTime+(15*60);
}
The idea is to (arbitrarily!) make the select input default to 19.30. 这个想法是(任意!)将选择输入默认为19.30。 The code above adds 上面的代码添加
selected = "selected"
to every option after 19:30, not just the 19:30 option. selected = "selected"
到19:30之后的每个选项,而不仅仅是19:30选项。 If I change the if statement slightly to be if ($perfTime = '19:30') { ...
ie, having a single =
instead of ==
it creates a set of options all with the value of 19:30. 如果我将if语句稍微更改为if ($perfTime = '19:30') { ...
即,用一个=
而不是==
它将创建一组选项,所有选项的值均为19:30。 What am I doing wrong? 我究竟做错了什么?
Short answer: Because every single echo operation uses the current value of $sel. 简短的回答:因为每个单独的回显操作都使用$ sel的当前值。 I assume it's initially blank, so the first N echos contain selected=''. 我假设它最初是空白的,所以前N个回声包含selected =''。 If test succeeds, $sel is set to "selected", and every later print includes selected='selected'. 如果测试成功,则将$ sel设置为“ selected”,并且以后的所有打印都包括selected ='selected'。 If you use $perfTime = '19:30', it's an assignment, so the test always succeeds, and $sel is always 'selected'. 如果您使用$ perfTime = '19:30',它是一个赋值,因此测试总是成功,并且$ sel总是被“选中”。
Quick fix: Add an else clause that sets $sel = ''. 快速解决方案:添加一个else子句,将$ sel =设置为。 However, there are other oddities that make me think this is only a code snippit (ie always using $thisTime for $perfTimestamp , rather than something loop indexed, so it always prints the same time?). 但是,还有其他奇怪的地方使我认为这只是一个代码片段(即,始终将$ thisTime用作$ perfTimestamp,而不是循环索引的东西,因此它总是在同一时间打印?)。
This is because you never reset $sel
. 这是因为您永远不会重置$sel
。
Try this instead: 尝试以下方法:
$sel = $perfTime == '19:30' ? 'selected' : '';
$sel
isn't explicitly intitialised anywhere, so it's maintaining its 'selected' value for each run through the loop. $sel
并未在任何地方显式初始化,因此它会为每次循环运行保持其“选定”值。
Try $sel = "";
尝试$sel = "";
as the first line in your loop as a quick fix. 作为循环中的第一行作为快速解决方案。
Hm, might be that you should do this: 嗯,可能是您应该这样做:
...
if ($perfTime == '19:30') {
$sel = 'selected="selected"';
}else{
$sel = "";
}
...
I think just having the 'selected' attribute present makes it selected. 我认为仅存在“ selected”属性就可以将其选中。
Oops, I forgot: And 糟糕,我忘记了:
echo '<option value="'. $perfTimeNumber .'" '.$sel.'>' .$perfTime .'</option>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.