Mr.Dev Mr.Dev -4 years ago 77
HTML Question

How to get data on database and display in "select" dropdown in view

I am a newbie to CodeIgniter, I have created a simple app that will fetch data from database and then display it in a

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");
$query = $this->db->get();

if ($query->num_rows >= 1){
foreach($query->result_array() as $row){
return $banklist;

My controller (home.php),

function index(){
$data['bankdata'] = $this->datamodel->getbanklist();
$this->load->view('viewdata', $data);

My view (viewdata.php),

<div class="containers">
<select name="bank">
<?php foreach($bankdata as $bank){
echo '<option value="'.$bank['id'].'">'.$bank['bank'].'</option>';
} ?>

My database structure (see here),

id    bank
0 Bank 1
1 Bank 2
2 Bank 3
3 Bank 4
4 Bank 5

Answer Source

Try this:


function getbanklist() {
    $query = $this->db->get();
    return $query;

In your view:

<select name="bank">
<?php foreach($bankdata->result() as $bank){ ?>
    <option value="<?php echo $bank->id ?>"><?php echo $bank->bank ?></option>
<?php } ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download