Codeigniter: Using set_select() if dropdown data c

2019-05-10 01:06发布

问题:

I have a dropdown <select> field which gets its values from the db. I'm currently looking for a way to use set_select() but have been unsuccessful. Here's my existing view:

<select name="userbase">
    <?php echo $userbase_list; ?>
</select>

The variable $userbase_list is a string containing all the <option value="">text</option> html to be inserted. Since this <select> is generated from the db, does that mean I can't use the CI set_select() anymore?

回答1:

$array_of_options= array("key_one"=>"val_one", "key_two"=>"val_two");

foreach($array_of_options as $k => $v)
{
    $selected = ($v === $db_result) ? 'selected=selected' : '';
    echo '<option '.$selected.' value='.$k.'>' . $v . '</option>';
}


标签: codeigniter