HTMLのリストボックスの内容を、PHPの2次元配列で指定する方法。
サンプル
PHPコード
<?php
$array = array(
array( "選択肢1", "選択肢1の値" ),
array( "選択肢2", "選択肢2の値", "selected" ),
array( "選択肢3", "選択肢3の値" ),
array( "選択肢4", "選択肢4の値", "selected" ),
array( "選択肢5", "選択肢5の値" ),
);
$sampleListBox = "<select name=\"listBoxName\" multiple size=\"5\">\n";
for ( $indexA = 0; $indexA < count( $array ); $indexA++ ) {
$sampleListBox .= "\t<option value=\"{$array[$indexA][1]}\"";
if ( $array[$indexA][2] ) {
$sampleListBox .= " selected=\"selected\"";
}
$sampleListBox .= ">";
$sampleListBox .= "{$array[$indexA][0]}";
$sampleListBox .= "</option>\n";
}
$sampleListBox .= "</select>\n";
echo "{$sampleListBox}";
?>
$array = array(
array( "選択肢1", "選択肢1の値" ),
array( "選択肢2", "選択肢2の値", "selected" ),
array( "選択肢3", "選択肢3の値" ),
array( "選択肢4", "選択肢4の値", "selected" ),
array( "選択肢5", "選択肢5の値" ),
);
$sampleListBox = "<select name=\"listBoxName\" multiple size=\"5\">\n";
for ( $indexA = 0; $indexA < count( $array ); $indexA++ ) {
$sampleListBox .= "\t<option value=\"{$array[$indexA][1]}\"";
if ( $array[$indexA][2] ) {
$sampleListBox .= " selected=\"selected\"";
}
$sampleListBox .= ">";
$sampleListBox .= "{$array[$indexA][0]}";
$sampleListBox .= "</option>\n";
}
$sampleListBox .= "</select>\n";
echo "{$sampleListBox}";
?>
↓↓↓出力結果↓↓↓
出力結果
HTMLコード
<select name="listBoxName" multiple="" size="5">
<option value="選択肢1の値">選択肢1</option>
<option value="選択肢2の値" selected="selected">選択肢2</option>
<option value="選択肢3の値">選択肢3</option>
<option value="選択肢4の値" selected="selected">選択肢4</option>
<option value="選択肢5の値">選択肢5</option>
</select>
<option value="選択肢1の値">選択肢1</option>
<option value="選択肢2の値" selected="selected">選択肢2</option>
<option value="選択肢3の値">選択肢3</option>
<option value="選択肢4の値" selected="selected">選択肢4</option>
<option value="選択肢5の値">選択肢5</option>
</select>