如何在编码器

时间:2017-02-10 10:12:11

标签: codeigniter

我的数据库中有两个表,一个是需求,另一个是users..i我的需求表中有那些用户名,选择框..如何将用户名id存储在数据库中..

这是我的视图

<div class="form-group">
  <label>Choose  Vendor</label>
  <select  class="form-control"  multiple 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>

这是我的控制器

public function requirement()
{
  $data["msg"]="";
  $this->load->model('RequirementModel');
  $data['user']=$this->RequirementModel->getusers();
  if($this->input->post())
  {
    $this->RequirementModel->add_requirement($this->input->post());
    redirect(base_url('index.php/Login/dashboard'));
  }
  $this->load->view('Requirements/requirements',$data);
}

这是我的模型

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

模型 add_requirement

public function add_requirement($data)
{
  $data=array('role_name'=>$post['role_name'],'vacancies'=>$po‌​st['vacancies'],'exp‌​erience'=>$post['exp‌​erience'],'jd'=>$pos‌​t['jd'],'hiring_cont‌​act_name'=>$post['hi‌​ring_contact_name'],‌​'hiring_contact_numb‌​er'=>$post['hiring_c‌​ontact_number'],'use‌​r_id'=>$user_id);
  $this->db->insert('requirements', $data);
}

怎么做?

谢谢

1 个答案:

答案 0 :(得分:0)

如下所示更改您的功能add_requirement并尝试;

function add_requirement($data)
{
  if(isset($data['user_id']) && !empty($data['user_id'])) {
    $data['user_id'] = implode(',',$user_id);
  }

  $data=array('role_name'=>$post['role_name'],'vacancies'=>$po‌​st['vacancies'],'exp‌​erience'=>$post['exp‌​erience'],'jd'=>$pos‌​t['jd'],'hiring_cont‌​act_name'=>$post['hi‌​ring_contact_name'],‌​'hiring_contact_numb‌​er'=>$post['hiring_c‌​ontact_number']);
      $this->db->insert('requirements', $data);
  }
}