にEの値をテストします。値が選択されている場合は、「選択済み」を印刷します。サイドノートとして
<select name="dropdown1" id="dropdown1">
<?php
$selected = 'red';// obviously replace with DB value.
for ($i = 0; $i < 5; $i++)
{
// start as normal
print '<option id="'.$options[$i]["ID"].'" value="'.$options[$i]["Value"].'"';
// if this one is selected, add 'selected' to the tag.
// NOTE: booleans in HTML do not need to have an attribute value.
// so selected="selected" is not necessary here.
if($options[$i]["Value"] == $selected) print ' selected';
// finish as normal
print '>'.$options[$i]["Name"].'</option>'."\n";
}
?>
</select>
:あなたはそれをforeachの使用している場合は、より小さく、よりコンパクトなために作る、そして多くの場合、高速なコードになります。あなたが選択ボックスはスティッキー作りたい
//this assumes that you want to iterate the whole options array
foreach($options as $option)
{
print '<option id="'.$option["ID"].'" value="'.$option["Value"].'"';
if($option["Value"] == $selected) print ' selected';
print '>'.$option["Name"].'</option>'."\n";
}
、正しいということでしょうか? 1つの値が選択され、ユーザーがページを投稿すると、前の値が選択されたまま返されることを意味しますか? – Drewdin