php 中的 $string 不在页面中回显

发布于 2024-11-30 20:22:44 字数 3593 浏览 0 评论 0原文

我不知道这段代码有什么问题

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-12-07 20:22:44
// try changing this line
<select name="select-choice-1" id="select-choice-1">

// to...
<select name="time" id="time">
// try changing this line
<select name="select-choice-1" id="select-choice-1">

// to...
<select name="time" id="time">
美胚控场 2024-12-07 20:22:44

只要您只想显示一个包含一天中所有小时的选择框,您基本上需要一个值从 0 到 23 的数组。对范围进行排序:

range(0, 23);

然后您需要知道是否已选择某些内容或不:

$value = $_POST['select-choice-1'];
$selected = $value == $hour;

让我们编译这个:

<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>

编辑:

该代码片段仅在选项框的输出上非常固定。当需要在整个页面上重复使用 $hour 值的显示时,格式:

$display = sprintf('%02d:00-%02d:00', $hour, $hour+1);

可以重复使用(如果格式发生变化,这不是很灵活)或者可以移动它变成一个自己的函数:

function hour_format($hour)
{
    return sprintf('%02d:00-%02d:00', $hour, $hour+1);
}

这​​个函数就可以在整个页面使用,只需要输入数字(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>

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:

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:

$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):

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文