php - How to use the ternary operator in an array? -
i trying figure out how can use ternary operator in array. want able check if value matches make option "selected"
.
here code:
<select id="time_out_1" name="data[<?= $id;?>][time_out]"> <?php $miltimes = array("<option value=\"0000\">0000</option>","<option value=\"0030\">0030</option>", "<option value=\"0100\">0100</option>", "<option value=\"0130\">0130</option>", "<option value=\"0200\">0200</option>", "<option value=\"0230\">0230</option>", "<option value=\"0300\">0300</option>", "<option value=\"0330\">0330</option>", "<option value=\"0400\">0400</option>", "<option value=\"0430\">0430</option>", "<option value=\"0500\">0500</option>", "<option value=\"0530\">0530</option>", "<option value=\"0600\">0600</option>", "<option value=\"0630\">0630</option>", "<option value=\"0700\">0700</option>", "<option value=\"0730\">0730</option>", "<option value=\"0800\">0800</option>", "<option value=\"0830\">0830</option>", "<option value=\"0900\">0900</option>", "<option value=\"0930\">0930</option>", "<option value=\"1000\">1000</option>", "<option value=\"1030\">1030</option>", "<option value=\"1100\">1100</option>", "<option value=\"1130\">1130</option>", "<option value=\"1200\">1200</option>", "<option value=\"1230\">1230</option>", "<option value=\"1300\">1300</option>", "<option value=\"1330\">1330</option>", "<option value=\"1400\">1400</option>", "<option value=\"1430\">1430</option>", "<option value=\"1500\">1500</option>", "<option value=\"1530\">1530</option>", "<option value=\"1600\">1600</option>", "<option value=\"1630\">1630</option>", "<option value=\"1700\">1700</option>", "<option value=\"1730\">1730</option>", "<option value=\"1800\">1800</option>", "<option value=\"1830\">1830</option>", "<option value=\"1900\">1900</option>", "<option value=\"1930\">1930</option>", "<option value=\"2000\">2000</option>", "<option value=\"2030\">2030</option>", "<option value=\"2100\">2100</option>", "<option value=\"2130\">2130</option>", "<option value=\"2200\">2200</option>", "<option value=\"2230\">2230</option>", "<option value=\"2300\">2300</option>", "<option value=\"2330\">2330</option>"); foreach($miltimes $miltime) { echo $miltime; } echo '</select>';?>
i found answer not quite sure how use in situation.
thanks in advance help.
instead of putting options tags in array, can put numbers , use php create tags you:
$miltimes = array('0000', '0030', '0100', '0130', '0200', '0230', '0300', '0330', '0400', '0430', '0500', '0530', '0600', '0630', '0700', '0730', '0800', '0830', '0900', '0930', '1000', '1030', '1100', '1130', '1200', '1230', '1300', '1330', '1400', '1430', '1500', '1530', '1600', '1630', '1700', '1730', '1800', '1830', '1900', '1930', '2000', '2030', '2100', '2130', '2200', '2230', '2300', '2330'); $select = '<select id="time_out_1" name="'.$data[$id]['time_out']."\">\n"; foreach ($miltimes $key => $value){ $selected = ($value == '0030') ? ' selected' : ''; $select .= "\t<option value=\"$value\"$selected>$value</option>\n"; } $select .= "</select>\n"; echo $select;
Comments
Post a Comment