Codeigniter - Form helper set_select()

寵の児 提交于 2019-12-10 16:45:10

问题


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


回答1:


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);



回答2:


Try this:

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

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




回答3:


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>



回答4:


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 ':''?>



回答5:


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



来源:https://stackoverflow.com/questions/8049458/codeigniter-form-helper-set-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!