0

我有这个 HTML 选择选项:

<select>
<option>Married</option>
<option>Single</option>
<option>In a relationship</option>
<option>Forever Alone</option>
</select>

并且 selected 选项基于 MySQL 结果,例如,如果$row['status'] = Marriedthen<option selected>Married</option>和其他选项在选项标签上没有“selected”字样。

如何使这个动态选择选项?

4

1 回答 1

5

一个例子:

// 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>';
}
于 2012-11-15T13:02:13.727 回答