Codeigniter - Form helper set_select()

14.8k Views Asked by At

How can I use set_select() (part of the form helper) on my dropdown array in codeigniter:

It is used to remember which option the user has selected.

$guide_options = array('investments' => 'Investments',
                       'wills' => 'Wills',
                       'tax-planning' => 'Tax planning',
                       'life-insurance' => 'Life insurance',
                       'currency-exchange' => 'Currency exchange',
                       'retirement-planning' => 'Retirement planning',
                       'international-healthcare' => 'International healthcare',
                       'savings-plans' => 'Savings plans',
                       'education-planning' => 'Education planning',
                       'sipps' => 'SIPPS',
                       'qrops' => 'QROPS',
                       'qnups' => 'QNUPS',
                       'mortgages' => 'Mortgages',
                       'other' => 'Other service'                       
                      );
echo form_dropdown('guide', $guide_options, 'investments');

Form helper guide here: http://codeigniter.com/user_guide/helpers/form_helper.html

5

There are 5 best solutions below

0
On BEST ANSWER

set_select you can use if you produce with html code not with helper.

Although you can do something like this

$selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';                        
echo form_dropdown('guide', $guide_options, $selected);
0
On

To use set_select you need to have a validation rule for the field in question. See my answer to a similar question here: CodeIgniter select_value in form_dropdown

0
On

You Don't have to Use set_select() as you are already defining selected item in from_dropdown()

form_dropdown('guide', $guide_options, 'investments');

For better understanding refer below code and output

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
0
On

Try this:

echo form_dropdown('guide', $guide_options,set_value('guide', 'investments'));

http://codeigniter.com/forums/viewthread/97665/

0
On

Here's a working example of set_select()

<?php echo set_select('my_select', $data['id'])?>

Full code:

<select class="form-control" required name="my_select">
    <option value="">- Select -</option>
    <?php foreach ($datas as $data): ?>
        <option value="<?php echo $data['id']?>" <?php echo set_select('my_select', $data['id'])?>><?php echo $data['name']?></option>
    <?php endforeach; ?>
</select>

Or you can use a pure PHP approach:

// Use this inside in a <option> generated by a foreach, like the example above.
<?php echo $id_city == $city['id']?' selected ':''?>