M5533 M5533 - 4 years ago 69
MySQL Question

Getting data in dropdown list from DB in Codeigniter

I have two tables in my database one is

users
and another one is
requirements
. So I have
user_id
in the
requirements
table. I want to get
user_id
in the requirements page dropdown list.

Here is my Controller code:

public function requirement() {
$data['user']=$this->LoginModel->getusers();
$data=array(
'role_name'=>$this->input->post('role_name'),
'vacancies'=>$this->input->post('vacancies'),
'experience'=>$this->input->post('experience'),
'jd'=>$this->input->post('jd'),
'hiring_contact_name'=>$this->input->post('hiring_contact_name'),
'hiring_contact_number'=>$this->input->post('hiring_contact_number'),
'user_id'=>$this->input->post('user_id')
);
}


Model code:

function getusers() {
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
echo $this->db->last_query();
return $query->result();
}


View code:

<div class="form-group">
<label>Select Vendor</label>
<select id="user_id" class="form-control" data-placeholder="user name" name="user_id" >
<option value="0"></option>

<?php foreach($user as $rows) { ?>
<option value="<?php echo $rows->user_id?>"><?php echo ucfirst($rows->first_name)?></option>
<?php } ?>
</select>
</div>


I got error in foreach loop


undefined variable data


please anyone help me..

Answer Source

simply use this function

    public function requirement() {
$this->load->model(LoginModel');
    $data['user']=$this->LoginModel->getusers();
$this->load->view('your_view_page',$data);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download