根据手册:
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
在你的情况下
<select style="width:25%;" name='state' <?php if(form_error('state') != '') { echo 'id=error'; } ?>>
<option value=''>State</option>
<?php foreach($states->result() as $row)
{
echo "<option value='$row->state_name' " . set_select('state', $row->state_name) . " >". $row->state_name."</option>";
} ?>
</select>
[更新]
<select style="width:25%;" name='state' <?php if(form_error('state') != '') { echo 'id=error'; } ?>>
<option value=''>State</option>
<?php
$defaultvalue = 'Nigeria'; //Please set default value here when nothing is selected before submit - change Nigeria
foreach($states->result() as $row)
{
echo "<option value='$row->state_name' " . set_select('state', $row->state_name, ((!isset(set_select('state', $row->state_name)) && ($row->state_name == $defaultvalue) ) ? TRUE : FALSE )) . " >". $row->state_name."</option>";
} ?>
</select>
如果您不想设置默认值,只需从 set_select 中删除 TRUE
如果您使用菜单,此功能允许您显示已选择的菜单项。第一个参数必须包含选择菜单的名称,第二个参数必须包含每个项目的值,第三个(可选)参数允许您将项目设置为默认值(使用布尔值 TRUE/FALSE)。