CodeIgniter Forums
form_dropdown issues - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: form_dropdown issues (/thread-48772.html)



form_dropdown issues - El Forum - 01-28-2012

[eluser]helmy[/eluser]
I'm having issues figuring out how to dynamically set the selected value on my drop down box. Does anyone know of a good tutorial for using drop down boxes?


form_dropdown issues - El Forum - 01-28-2012

[eluser]smilie[/eluser]
It is all in the user guide:

http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

"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."

So just set as third param, dymically what you want to be pre-selected.

Cheers,
Smilie


form_dropdown issues - El Forum - 01-28-2012

[eluser]helmy[/eluser]
I've looked over the user guide and these forums several times. I'm sure it's something simple and I'm over looking it. I'm using drop down box to edit the position an item shows up in a menu. I want the current position the item is to be the selected value. Here's what I currently have in my code.

Model:
Code:
###
# This will return an array with each page position.
###
function page_position_count($subject_id) {
  $page_set = $this->db->from('pages')->where('subject_id', $subject_id)->get();

  if($page_set->num_rows() > 0) {
   foreach ($page_set->result() as $page) {
    $data[$page->id] = $page->position;
   }
   return $data;
  }
}

View:
Code:
<p> Position: &lt;?php echo form_dropdown('position', $page_count, /* Mystical 3rd param */); ?&gt; </p>

Controller:
Code:
function edit_page($sel_subject = NULL, $sel_page = NULL) {
  $this->load->model('validate_model');
  $this->validate_model->confirm_logged_in();
  
  
  $header_data['title'] = 'Widget Corp: Edit Page';
  $data['subjects'] = $this->get_func_model->get_all_subjects();
  $data['pages'] = $this->get_func_model->get_pages_for_subject($sel_subject, false);
  $data['private_nav'] = $this->nav_model->private_navigation($sel_subject);
  $data['cur_page'] = $this->get_func_model->get_page_by_id($sel_page);
  $data['cur_subject'] = $this->uri->segment(3);

  //Passes the array of positions to the view
  $data['page_count'] = $this->get_func_model->page_position_count($sel_subject);
  
  $this->load->view('includes/header', $header_data);
  $this->load->view('edit_page_view', $data);
  $this->load->view('includes/footer');
}




form_dropdown issues - El Forum - 01-28-2012

[eluser]helmy[/eluser]
I figured it out. I used
Code:
$this->uri->segment(4)
as the third param. which is the page id and is used for the array key. I thought I already tried this, but I guess I just thought about doing it and for some reason thought it wouldn't work. I guess I just needed to step away from the computer for a bit. Tongue

Thanks for your reply Smilie.