[eluser]smilie[/eluser]
Hi,
I had to brake my head around this one as well, but got it finally (tho' not sure this is the best solution - but it works for me).
Note: I am only showing you parts of code which are relevant, there is more to it. But here bellow I do:
- get locations from model;
- present found locations in the drop down in view (keeping location_id as dropdown ID (!!) which is important to know which location ID was chosen ;-)
Good, my controller:
Code:
# Get all locations
$this->load->model('location_model');
$data['result']['locations'] = $this->location_model->getLocations();
Note: You could pass extra param's to your model (like ID or something);
My model
Code:
function getLocations()
{
# Get all locations
$this->db->select('location_id,location_name');
$this->db->from('location');
$this->db->order_by('location_name','ASC');
$query = $this->db->get();
$res = $query->result_array();
if($query->num_rows() > 0)
{
$result = $res;
}
else
{
# No results :(
$result = FALSE;
}
# return data
return $result;
}
Note: if you would pass extra info to model, then you could use it to add WHERE in the query;
My view
Code:
<?php
$locations = array();
foreach($result['locations'] as $key=>$val)
{
$tmp = array($val['location_id']=>$val['location_name']);
$locations = $locations + $tmp;
}
$extra = "style='width:90%;'";
echo form_dropdown('stock_location_id', $locations, $result['stock_location_id'], $extra);
?>
Note: Oke, this was tricky part. What I did in here is re-create new array based on the results from the model so it 'fits' the needs of form_dropdown. This way, form_dropdown creates this:
Code:
<select style="width: 90%;" name="stock_location_id">
<option value="5">Location 1</option>
<option value="6">Location 2</option>
<option value="2">Location 3</option>
<option value="4">Location 4</option>
<option selected="selected" value="1">Location 5</option>
<option value="7">Location 6</option>
</select>
Do bare in mind, that I also post $result['stock_location_id'] from the controller which holds the current chosen location in DB (so that this one is "selected" by default on page load).
Hope this helps :-)
Cheers,
Smilie