你可以这样做:
<?php
if (isset($_POST['id']) && ((int) $_POST['id'])) != 0) {
$id = $_POST['id'];
}
//connect to db
//get the responce of the query "SELECT `type` FROM `ads` WHERE `id` = $id"
//put it in a variable named $type
?>
<form method="POST" action="">
<select size='1' name='type'>
<option value="" <?=($type == '') ? 'selected="selected"' : ''?>></option>
<option value="1" <?=($type == 1) ? 'selected="selected"' : ''?>>Open</option>
<option value="0" <?=($type == 2) ? 'selected="selected"' : ''?>>Closed</option>
</select>
</form>
编辑:如果您有一个没有太多选项的选择,这很好,否则您应该使用类似 foreach 语句的想法。
对于许多选项,您可以使用它:
<?php
$options = array('Open' => 1, 'Closed' => 0, 'Select an option' => ''); ?>
<form method="POST" action="">
<select size='1' name='type'>
<?php
foreach($options as $label => $option) {
if ($type == $option) {
$checked = 'selected="selected"';
}
else {
$checked = '';
}
?>
<option value="<?=$option?>" <?=$checked?>><?=$label?></option>
<?php
}
?>
</select>
</form>