简体   繁体   English

生成的选项选择时间无法正确保存

[英]Generated option select time does not save properly

I have code that generates a select dropdown with times from 8:00am-6:00pm. 我的代码生成一个选择下拉列表,时间从上午8:00到下午6:00。 Upon selecting an option, and saving the form, the times are saved in the database properly. 选择选项并保存表单后,时间将正确保存在数据库中。 HOWEVER, I cannot retrieve the times automatically. 但是,我无法自动检索时间。 This is my code so far : 到目前为止这是我的代码:

<select value="<?php echo $starttime; ?>" name="data[InvoiceTime][<?php echo $key;?>][starttime]" id="starttime_<?php echo $key+1?>" class="form-control" autocomplete="off">
    <?php
        $time = mktime(0, 0, 0, 1, 1);
        for ($i = 28800; $i < 42600; $i += 900) {  // 1800 = half hour, 86400 = one day
        printf('<option value="%1$sam">%1$sam</option>',
        date('g:i', $time + $i), date('g:i a', $time + $i + 1800));
        }
        for ($i = 43200; $i < 65000; $i += 900) {  // 12pm-6pm
        printf('<option value="%1$spm">%1$spm</option>',
        date('g:i', $time + $i), date('g:i a', $time + $i + 1800));
        }
        ?>
    </select>

Desired output : select "9:00am" and save form 9:00am is saved to db(which happens) reload form  9:00 appears preselected due to data saved in db

Actual output : select "9:00am" and save form 9:00am is saved to db reload form 8:00am appears(default value)

The following code works, but does not generate the option values : 以下代码有效,但不生成选项值:

<select value="<?php echo $starttime; ?>" name="data[InvoiceTime][<?php echo $key;?>][starttime]" id="starttime_<?php echo $key+1?>" class="form-control" autocomplete="off">
        <option value="8:00am" <?= ($item['starttime']) == '8:00am' ? 'selected' : '' ?>>8:00am</option>
        <option value="9:00am" <?= ($item['starttime']) == '9:00am' ? 'selected' : '' ?>>9:00am</option>
</select>

You just needed to move your check for the value into the existing code. 您只需将检查值移动到现有代码中即可。

However, I have simplified your code quite a bit; 但是,我已经简化了你的代码; I'm not sure what you were trying to accomplish with your printf() statements. 我不确定你用printf()语句想要完成什么。 gmdate() is happy to work with just seconds (you don't want to use date() as it can deliver unexpected results, based on your current time zone.) If an <option> element doesn't have a value attribute, the contents are used instead, so I've removed it. gmdate()乐意只使用秒 (你不想使用date()因为它可以根据你当前的时区提供意想不到的结果。)如果<option>元素没有value属性,改为使用内容,所以我删除了它。 You should always always separate your PHP from your HTML (ideally more than I've done here, but this is a start.) 你应该总是将你的PHP与HTML分开(理想情况下,这比我在这里完成的要多,但这是一个开始。)

And in case you aren't familiar with them, here is some info on the ternary statement and heredoc blocks . 如果你不熟悉它们,这里有关于三元语句heredoc块的一些信息。

<?php
$start    = 28800;
$stop     = 65000;
$interval = 900;
$options  = "";
for ($seconds = $start; $seconds <= $stop; $seconds += $interval) {
    $time     = gmdate("g:ia", $seconds);
    $selected = ($item["starttime"] === $time) ? " selected" : "";
    $options .= sprintf("<option %s>%s</option>", $selected, $time);
}

echo <<< HTML
<select value="$starttime" name="data[InvoiceTime][$key][starttime]" id="starttime_$key" class="form-control" autocomplete="off">
$options
</select>
HTML;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM