我有这个HTML选择选项:
<select>
<option>Married</option>
<option>Single</option>
<option>In a relationship</option>
<option>Forever Alone</option>
</select>
并且所选选项基于MySQL结果,例如$row['status'] = Married
然后<option selected>Married</option>
,而其他选项在选项标记上没有“选定”字。
如何制作这个动态选择的选项?
答案 0 :(得分:4)
一个例子:
// 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>';
}