If you would like to use the form dropdown helper, you need to pass a associative array rather than object. You something in your controller like:
$data['cat'] = array();
foreach($categories as $row){
$cat[$row->cat_id] = $row->cat_name;
}
$this->load->view('your_view', $data);
//Now in your view
echo form_dropdown('category', $cat, set_value('category'), 'id="category");
I can’t seem to get the drop down to select the active ID in my form. I’ve tried passing both the value and the text as the selectes value but it won’t work. Here’s where I’m at right now (also I’m using Colin’s CRUD example for my form and running the form as a module):
In my model:
function getTypeList()
{
$this->db->select('pgTypeID,typeName');
$this->db->order_by('typeOrder', 'asc');
$query = $this->db->get('pageTypes');
if ($query->num_rows() > 0)
{
//create this array for view loop
$typelist = $query->result();
}
In my controller:
$getTypesList = $this->form_model->getTypeList();
foreach($getTypesList as $row){
$typelist[$row->pgTypeID] = $row->typeName;
}
$form['typelist'] = $typelist;
In my view:
<?php echo form_dropdown('pgTypeID', $typelist, set_value($fields['pgTypeID'])); ?>
The dropdown populates correctly and I have echo’ed my variables in various places and they exist, $fields[‘pgTypeID’] follows the current active ID
what am I doing wrong?
ps: I even tried setting the selected value manually using set_value(‘2’) and it still won’t work.