Christian
Christian

Reputation: 269

retrieve data from db to dropdown

I need to fetch data from database and put it in my dropdownlist as choices so far nothing is showing in the dropdownlist

MODEL

function getAllGroups()
{


    $query = $this->db->query('SELECT firstname FROM tblsec');


    return $query->result();


}

VIEW

        foreach($groups as $row)
        { 
          echo '<option value="'.$row->firstname.'">'.$row->firstname.'</option>';
        }
        ?>
        </select>

CONTROLLER

public function salesorders() {
    if ($this->session->userdata('logged_in')) {
        $this->header2();
        $data['groups'] = $this->secretary_model->getAllGroups();
        $this->load->view('secretary/transactions');
    } else {
        redirect('secretary/sec_login_view');
    }
}

Upvotes: 0

Views: 47

Answers (2)

Jeremy Jackson
Jeremy Jackson

Reputation: 2257

Try this:

Controller

$data['groups'] = $this->secretary_model->getAllGroups();
$this->load->view('secretary/transactions', $data);

View

$options = array();

foreach($groups as $group){
    $options[$group->firstname] = $group->firstname;
}

echo form_dropdown('dropdownName', $options);

Upvotes: 0

Micaiah Wallace
Micaiah Wallace

Reputation: 1146

You didn't pass any data to your view.

$this->load->view() has an optional second parameter to supply data to your view from the controller. e.g. $this->load->view('secretary/transactions', $data)

You also don't define $data anywhere until $data['groups'] (Unless you just cut out some code)

Finally, the view will receive the passed data as $groups because that's what you are setting it to in the $data variable when you say $data['groups'], it turns the keys into variables.

Also, I would try outputting your database query on the page to see if it's successfully querying before putting it in the SELECT tag (just for easier debugging)

Upvotes: 1

Related Questions