3

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 
0

2 Answers 2

3

Try this:

Model:

function getbanklist() { $this->db->select("id,bank"); $this->db->from('bank'); $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 } ?> </select> 
Sign up to request clarification or add additional context in comments.

Comments

0

How to pass database value to view page drop down list in codeingniter.

This is my HTML code:

<div class="form-group"> <select name="department" id="department"> <?php foreach($bankdata as $key => $value) { ?> <option value="<?php echo $value['dept_id']; ?>"><?php echo $value['managers_name']; ?></option> <?php } ?> </select> </div> 

This is my controller code:

public function department() { $this->load->model('insert_model'); $data['bankdata'] = $this->insert_model->getbanklist(); //$this->load->view('login_view', $data); $this->load->view('login_view',$data); } 

This is my model code:

function category_name_get() { $this->load->database(); $query=$this->db->get('dept');//employee is a table in the database return $query->result(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.