我有这个 HTML 选择选项:
<select>
<option>Married</option>
<option>Single</option>
<option>In a relationship</option>
<option>Forever Alone</option>
</select>
Run Code Online (Sandbox Code Playgroud)
并且选定的选项基于 MySQL 结果,例如,如果$row['status'] = Marriedthen<option selected>Married</option>和其他选项在选项标签上没有“selected”字样。
如何制作这个动态选择的选项?
一个例子:
// these can also come from a database
$statuses = array( 'Married', 'Single', 'In a relationship' );
foreach ( $statuses as $status ) {
echo '<option' . ( $row['status'] == $status ? ' selected' : '' ) . '>';
echo $status;
echo '</option>';
}
Run Code Online (Sandbox Code Playgroud)