5

Im having a few problems with the form_dropdown function in CodeIgniter .... My application is in 2 parts, a user goes in, enters a form and submits it .... once its submitted a admin can go in and edit that persons form and then save it to database.

So, to display the dropdown in the initial form, im using the following ( all the options in the dropdown are coming from the database )

Model:

 function get_salaries_dropdown() { $this->db->from($this->table_name); $this->db->order_by('id'); $result = $this->db->get(); $return = array(); if($result->num_rows() > 0){ $return[''] = 'please select'; foreach($result->result_array() as $row){ $return[$row['id']] = $row['salaryrange']; } } return $return; } 

Then in the Controller:

$data['salaries'] = $this->salary_expectation->get_salaries_dropdown(); 

Then finally the View:

<?php echo form_dropdown('salaries', $salaries, set_value('salaries', $salaries)); ?> 

That bit works perfect in displaying the dropdown filled with values for the user to select.

So, when the user selects a value, then hits save, its saved to the database.

On the Edit page which the admin see's, im using the same code to display the dropdown filled with options, but how do i get it to automatically choose the one thats been selected by the user in the initial stage?

Cheers,

2
  • From the codeigniter documentation - The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. What is this set_value method you are using in third parameter? Commented Dec 2, 2010 at 2:31
  • There is this codeigniter solution i found github.com/thiswolf/codeigniter-enum-select-boxes Commented Jul 7, 2013 at 17:00

5 Answers 5

3

According to Codeigniter documentation

The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you.

Your admin controller should have something like

$data['selected'] = $this->salary_expectation->get_salary_selected(); 

According to this, the admin view should be like this

<?php echo form_dropdown('salaries', $salaries, $selected_value); ?> 
Sign up to request clarification or add additional context in comments.

Comments

1

one nasty solution to select the <option> element of <select> generated by form_dropdown() function of the form_helper is using the post input sended. I made this because any solutions I found doesn't display the value that the user select in the form neither set_selected nor set_vaule. Well, in my controller I have:

$countries = $this->country_model->get_dropdown_array(); // The array have something like $countries[COUNTRY_ID] = COUNTRY_NAME $data['countries']=$countries; 

In my view:

$selected_country = $this->input->post('country'); echo form_dropdown('country',$countries,$selected_country); 

And works fine !!! :)

Comments

0

The form_dropdown function has a third parameter for the selected option. Use it like this:

<?php echo form_dropdown('piece_type', array( 'type1' => 'Firts type', 'type2' => 'Second option' $selected_value, 'id = "piece_type"') ?> 

Comments

0

I had the same problem but i have overcome on this problem using code igniter syntex. Here is the solution. Fisrt step Before the loop initialize two arrays

$options = array(); $select = array(); 

Then in the loop write this instruction

foreach($result->result_array() as $row) { /////////Your Condition //////////// if($row['id'] == $myarray['mycolumn']) { $options [$row['id']] = $row['salaryrange']; $select = $row['id'] ; }else{ $options [$row['id']] = $row['salaryrange']; } } 

Now

echo form_dropdown('dropdown_name' , $options , $select); 

It is working ok

Comments

0

For update case, you have pass corresponding value to view, if passing variable is like $ind_post(from controller ) then write this code like:

<?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,''); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.