I am a newbie to CodeIgniter, I have created a simple app that will fetch data from database and then display it in a <SELECT> dropdown. I'm trying to get data from a specific field from database to my view. So far, I have tried the code below (not working):
My model (datamodel.php),
function getbanklist() {
$banklist = array();
$this->db->select("id, bank");
$this->db->from('bank');
$query = $this->db->get();
if ($query->num_rows >= 1){
foreach($query->result_array() as $row){
$banklist[$row['id']]=$row['bank'];
}
return $banklist;
}
}
My controller (home.php),
function index(){
$data['bankdata'] = $this->datamodel->getbanklist();
$this->load->view('viewdata', $data);
}
My view (viewdata.php),
<tr>
<th>BANK</th>
<td>
<div class="containers">
<select name="bank">
<?php foreach($bankdata as $bank){
echo '<option value="'.$bank['id'].'">'.$bank['bank'].'</option>';
} ?>
</select>
</div>
</td>
</tr>
My database structure (see here),
id bank ------------ 0 Bank 1 1 Bank 2 2 Bank 3 3 Bank 4 4 Bank 5