从Codeigniter中的DB获取下拉列表中的数据

时间:2017-02-06 11:30:33

标签: mysql codeigniter

我的数据库中有两个表,一个是users,另一个是requirements。所以我在user_id表中有requirements。我想在需求页面下拉列表中获取user_id

这是我的控制器代码:

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

型号代码:

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

查看代码:

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

我在foreach循环中遇到错误

  

未定义的可变数据

请有人帮助我..

2 个答案:

答案 0 :(得分:0)

只需使用此功能

    public function requirement() {
$this->load->model(LoginModel');
    $data['user']=$this->LoginModel->getusers();
$this->load->view('your_view_page',$data);
}

答案 1 :(得分:0)

try this 

    public function requirement() {
$this->load->model(LoginModel');
   $data=array();
    $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')
    );

$this->load->view('your_view_page',$data);
}