[英]$string in php not echo in the page
I don't know what is wrong with this code 我不知道这段代码有什么问题
<?php
$items = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23');
$time = array_search((empty($_POST['time']) ? '00' : $_POST['time']), $items) + 1; // Change '00' to whatever the default page is
// To name the labels, you can add keys for your values like so:
$items = array('00:00 - 01:00' => '00', '01:00 - 02:00' => '01', '02:00 - 03:00' => '02', '03:00 - 04:00' => '03', '04:00 - 05:00' => '04', '05:00 - 06:00' => '05', '06:00 - 07:00' => '06', '07:00 - 08:00' => '07', '08:00 - 09:00' => '08', '09:00 - 10:00' => '09', '10:00 - 11:00' => '10', '11:00 - 12:00' => '11', '12:00 - 13:00' => '12', '13:00 - 14:00' => '13', '14:00 - 15:00' => '14', '15:00 - 16:00' => '15', '16:00 - 17:00' => '16', '17:00 - 18:00' => '17', '18:00 - 19:00' => '18', '19:00 - 20:00' => '19', '20:00 - 21:00' => '20', '21:00 - 22:00' => '21', '22:00 - 23:00' => '22', '23:00 - 00:00' => '23');
$time = (empty($_POST['time']) ? '00:00 - 01:00' : array_search($_POST['time'], $items)); // Replace 'home' with whatever the default page is
?>
<div data-role="fieldcontain">
<select name="select-choice-1" id="select-choice-1">
<option>เลือกเวลา</option>
<?php foreach ($items as $key => $value): ?>
<option value="<?= $value ?>"<?= $key === $_POST['time'] ? ' ' : '' ?>> <?= $key ?></option>
<?php endforeach; ?>
</select>
</div>
and here is what the result of the above code 这是上面代码的结果
<div data-role="fieldcontain">
<select name="select-choice-1" id="select-choice-1">
<option>เลือกเวลา</option>
<option value="00"> 00:00 - 01:00</option>
<option value="01"> 01:00 - 02:00</option>
<option value="02"> 02:00 - 03:00</option>
<option value="03"> 03:00 - 04:00</option>
<option value="04"> 04:00 - 05:00</option>
<option value="05"> 05:00 - 06:00</option>
<option value="06"> 06:00 - 07:00</option>
<option value="07"> 07:00 - 08:00</option>
<option value="08"> 08:00 - 09:00</option>
<option value="09"> 09:00 - 10:00</option>
<option value="10"> 10:00 - 11:00</option>
<option value="11"> 11:00 - 12:00</option>
<option value="12"> 12:00 - 13:00</option>
<option value="13"> 13:00 - 14:00</option>
<option value="14"> 14:00 - 15:00</option>
<option value="15"> 15:00 - 16:00</option>
<option value="16"> 16:00 - 17:00</option>
<option value="17"> 17:00 - 18:00</option>
<option value="18"> 18:00 - 19:00</option>
<option value="19"> 19:00 - 20:00</option>
<option value="20"> 20:00 - 21:00</option>
<option value="21"> 21:00 - 22:00</option>
<option value="22"> 22:00 - 23:00</option>
<option value="23"> 23:00 - 00:00</option>
</select>
</div>
sorry I didn't explain it right at the first place, 抱歉,我一开始并没有解释清楚,
what I'm trying to achieve here is that 我想在这里实现的是
I want to echo the time in the option 我想在选项中回显时间
I was using <?php echo $time; ?>
我在用
<?php echo $time; ?>
<?php echo $time; ?>
and this doesn't work <?php echo $time; ?>
这不起作用
I already use the value with other things 我已经在其他东西上使用了价值
<?php echo $_POST["select-choice-1"]; ?> <<< this one been use with the other element and it display the value
Thanks 谢谢
EDIT 01: 编辑01:
Sorry what I want is that if someone choose option value="03" then it will display on the the place where I want 03:00 - 04:00 抱歉,如果有人选择option value =“ 03”,它将显示在我想要的位置03:00-04:00
// try changing this line
<select name="select-choice-1" id="select-choice-1">
// to...
<select name="time" id="time">
As long as you're only wanting to display a select box that has all the hours of one day, you basically need an array with the values from 0 to 23. Sort of a range: 只要您只想显示一天中所有时间的选择框,则基本上需要一个数组,其值从0到23。对范围进行排序:
range(0, 23);
Then you need to know if something has been selected or not: 然后,您需要知道是否已选择某项:
$value = $_POST['select-choice-1'];
$selected = $value == $hour;
Let's compile this: 让我们编译一下:
<div data-role="fieldcontain">
<select name="select-choice-1" id="select-choice-1">
<option>เลือกเวลา</option>
<?php
foreach (range(0, 23) as $hour)
{
$selected = $hour == $_POST['select-choice-1'] ? ' selected="selected"' : '';
$display = sprintf('%02d:00-%02d:00', $hour, $hour+1);
echo '<option value="', $hour, '"', $selected, '>', $display, '</option>';
}
?>
</select>
</div>
Edit: 编辑:
That code-snippet is pretty fixed on the output of the option box only. 该代码段仅固定在选项框的输出上。 When there is need to re-use the display of an
$hour
value across the page, the formatting: 当需要在页面上重复使用
$hour
值的显示时,格式为:
$display = sprintf('%02d:00-%02d:00', $hour, $hour+1);
could be re-used (which is not very flexible if the format changes) or it can be moved into a function of it's own: 可以重新使用(如果格式更改,灵活性不高),也可以将其移至其自身的功能中:
function hour_format($hour)
{
return sprintf('%02d:00-%02d:00', $hour, $hour+1);
}
This function then can be used in the whole page, it only needs the numerical input (0-23): 然后,此函数可以在整个页面中使用,只需要数字输入(0-23):
<div data-role="fieldcontain">
Currently selected: <b><?php echo hour_format($_POST['select-choice-1']); ?></b>
<select name="select-choice-1" id="select-choice-1">
<option>เลือกเวลา</option>
<?php
foreach (range(0, 23) as $hour)
{
$selected = $hour == $_POST['select-choice-1'] ? ' selected="selected"' : '';
$display = hour_format($hour);
echo '<option value="', $hour, '"', $selected, '>', $display, '</option>';
}
?>
</select>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.