Welcome Guest, Not a member yet? Register   Sign In
form_dropdown issues
#1

[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?
#2

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

http://ellislab.com/codeigniter/user-gui...elper.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
#3

[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');
}

#4

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




Theme © iAndrew 2016 - Forum software by © MyBB