How to get form_dropdown () to show the selected value in Codeigniter?

I am trying to populate a dropdown from a database. In my view file I have the following code

$batch= $query ['batch']; // I pull this data from a separate model echo form_dropdown('shirts', $options, $batch); 

Now the drop-down list fills the data, but the problem is that I do not get the value - "$ batch" is automatically selected when the page loads. Interestingly, if I drive away $ batch, the correct data is displayed elsewhere on the page, which means that $ batch is fine.

Here is my controller

 function update($id){ $this->load->model('mod_studentprofile'); $data['query']= $this->mod_studentprofile->student_get($id); $data['options']= $this->mod_studentprofile->batchget(); $data['tab'] = "Update Student Information"; $data['main_content']='update_studentprofile'; $this->load->view('includes/template',$data); } 

And here is my model

  function batchget() { $this->db->select('batchname'); $records=$this->db->get('batch'); $data=array(); foreach ($records->result() as $row) { $data[$row->batchname] = $row->batchname; } return ($data); } 

Could you help me solve this problem. I want the value of "$ batch" to be automatically selected from the drop-down list when the page loads.

Thanks at Advance.

EDit ... my model for student_get ($ id)

  function student_get($id) { $query=$this->db->get_where('student',array('studentid'=>$id)); return $query->row_array(); } 

Thanks:)

+4
source share
2 answers

I think that what is likely happening is that the value in $ batch may correspond to the rendering in the drop-down list, and not the actual key in $ options for this particular parameter, which will be part of value = "" html.

eg...

 // this wouldn't select 'foo' as you may be thinking $options => array('0' => 'foo', '1' => 'bar'); $batch = 'foo'; echo form_dropdown('shirts', $options, $batch); // this would select foo $options => array('foo' => 'foo', 'bar' => 'bar'); $batch = 'foo'; echo form_dropdown('shirts', $options, $batch); 

Edit in response to OP comment:

The batchget () method looks like this: it returns your $ options array in the appropriate format, and your student_get () method returns row_array. It looks like in the view, you assign the value of one of the keys returned by the student_get method as the selected value stored in $ batch, which is then passed as the third argument to form_dropdown ().

It all seems right. As long as the value of $ batch is really one of the keys of the array, which is in $ options, then form_dropdown () will set one of the drop-down options as the selected one.

+5
source

Debugging

var_dump() $options , var_dump() $batch , look at two and see where you did wrong.

The third parameter should be the key value, not the label value.

Anthony Jack is probably right.

+1
source

All Articles