Codeigniter - Form helper set_select ()

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

Used to remember the option that 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 Reference: http://codeigniter.com/user_guide/helpers/form_helper.html

+4
source share
5 answers

you can use set_select if you are not creating the html code using the 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); 
+11
source

Try the following:

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

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

+3
source

You do not need to use set_select () since you already define the selected item in from_dropdown ()

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

For a better understanding see 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> 
+1
source

Here is 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 ':''?> 
+1
source

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
source

Source: https://habr.com/ru/post/1380158/


All Articles