php 中的 $string 不在页面中回显
我不知道这段代码有什么问题
<?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>
,这是上面代码的结果
<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>
抱歉我一开始没有正确解释它,
我在这里想要实现的是
我想回显时间 选项中
在我使用 这不起作用
我已经将该值与其他东西一起使用
<?php echo $_POST["select-choice-1"]; ?> <<< this one been use with the other element and it display the value
谢谢
编辑01:
抱歉我想要的是,如果有人选择选项 value="03" 那么它将显示在我所在的位置想要 03:00 - 04:00
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; ?>
and this doesn't work
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您只想显示一个包含一天中所有小时的选择框,您基本上需要一个值从 0 到 23 的数组。对范围进行排序:
然后您需要知道是否已选择某些内容或不:
让我们编译这个:
编辑:
该代码片段仅在选项框的输出上非常固定。当需要在整个页面上重复使用
$hour
值的显示时,格式:可以重复使用(如果格式发生变化,这不是很灵活)或者可以移动它变成一个自己的函数:
这个函数就可以在整个页面使用,只需要输入数字(0-23):
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:
Then you need to know if something has been selected or not:
Let's compile this:
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: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:
This function then can be used in the whole page, it only needs the numerical input (0-23):