How to get database data and display it in the Select dropdown (Codeigniter)

I am new to CodeIgniter, I created a simple application that will fetch data from a database and then display it in a dropdown <SELECT>

. I am trying to get data from a specific field from a database into my view. So far I have tried the code below (doesn't work):

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
+3


source to share


2 answers


Try the following:

Model:

function getbanklist() {
    $this->db->select("id,bank");
    $this->db->from('bank');
    $query = $this->db->get();
    return $query;
}

      



In your opinion:

<select name="bank">
<?php foreach($bankdata->result() as $bank){ ?>
    <option value="<?php echo $bank->id ?>"><?php echo $bank->bank ?></option>
<?php } ?>
</select>

      

+3


source


How to pass database value to view dropdown page in coding.

This is my HTML:

<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();
}

      

0


source







All Articles