3

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?

3
  • 3
    Wait why are you storing them in a string like that if you already have the form helper built into CI? Commented Feb 14, 2012 at 21:09
  • Holy crap! You're right! What the hell was I thinking? Commented Feb 14, 2012 at 21:11
  • I noticed that set_select() doesn't work if you place a default value in form_dropdown. I don't want to place a default but I need to get to the 4th parameter so I can apply a class or any other HTML attribute. Is there a way to bypass setting the default entirely? Commented Feb 14, 2012 at 21:46

1 Answer 1

3
$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>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

$selected needs to be concated to the string too. I fixed it for you :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.