I am generating an html dropdown and I currently have the code to select the data from the database and generate the result in the view? Is this ok, or should I be placing it in the controller somehow. If so, how do I call that data from the view? This is my code:
<select id="f_treeindex">
<?php
$query = $this->db->query('SELECT id, tree_name FROM trees');
foreach ($query->result() as $row)
{
echo '<option value="' . $row->id . '">' . $row->tree_name . '</option>';
};
?>
</select>
I want to keep as much DB data out of my views as possible.
REVISED:
MODEL:
function get_tree(){
$query = $this->db->query('SELECT id, tree_name FROM trees');
return $query->result_array();
}
CONTROLLER:
$data['trees'] = $this->Model_form->get_tree();
if ($this->form_validation->run() == FALSE)
{
$this->load->view('view_form_group', $data);
}
VIEW:
<?php
foreach($trees as $tree){
echo '<option value="' . $tree->id . '">' . $tree->tree_name . '</option>';
}
?>