简体   繁体   中英

$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; ?> 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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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