如何在codeigniter中的数据库中存储多个选择框值?

如何在codeigniter中的数据库中存储多个选择框值?

问题描述:

I have one requirements form in my page..in that form i have users dropdown with select box. I want to select multiple users at a time and need to store selected users in database.

Controller:

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

    $insert = 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 is my foreign key id from users table);
    );
    $this->LoginModel->add_requirement($insert);
}

Model:

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

View page

<div class="form-group">
    <label>Choose Vendor</label>
    <select id="chkveg"class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >
        <option value="0"></option>
        <?php
        print_r($user);
        foreach ($user as $rows)
        {
            ?>
            <option value="<?php echo $rows->user_id ?>">
                <?php echo ucfirst($rows->first_name) ?>
            </option>
            <?php
        }
        ?>
    </select>
</div>

我的页面中有一个需求表单。在该表单中,我有用户下拉列表和选择框。 我想一次选择多个用户,并且需要将所选用户存储在数据库中。 p>

控制器: p>

 公共功能要求()  
 {
 $ this-&gt; load-&gt; model('LoginModel'); 
 $ data ['user'] = $ this-&gt; LoginModel-&gt; getusers(); 
 $ this-&gt  ; load-&gt; view('Admin / requirements',$ data); 
 
 $ insert = array(
'role_name'=&gt; $ this-&gt; input-&gt; post('role_name'),  
'vacancies'=&gt; $ this-&gt; input-&gt; post('vacancies'),
'experience'=&gt; $ this-&gt; input-&gt; post('experience'),
  'jd'=&gt; $ this-&gt; input-&gt; post('jd'),
'hiring_contact_name'=&gt; $ this-&gt; input-&gt; post('hiring_contact_name'),
'hiring_contact_number  '=&gt; $ this-&gt; input-&gt; post('hiring_contact_number'),
'user_id'=&gt; $ this-&gt; input-&gt; post('user_id')//这是我的外键 来自用户表的id); 
); 
 $ this-&gt; LoginModel-&gt; add_requirement($ insert)  ; 
} 
  code>  pre> 
 
 

模型: p>

  function getusers()
 {
 $ this-&gt  ; db-&gt; select('*'); 
 $ this-&gt; db-&gt; from('users'); 
 $ query = $ this-&gt; db-&gt; get(); 
  // echo $ this-&gt; db-&gt; last_query(); 
返回$ query-&gt; result(); 
} 
  code>  pre> 
 
 

查看页面 p>

 &lt; div class =“form-group”&gt; 
&lt; label&gt;选择供应商&lt; / label&gt; 
&lt; select id =“chkveg”class =  “form-control”multiple class =“form-control”data-placeholder =“user name”name =“user_id []”&gt; 
&lt; option value =“0”&gt;&lt; / option&gt; 
&lt;  ;?php 
 print_r($ user); 
 foreach($ user as $ rows)
 {
?&gt; 
&lt; option value =“&lt;?php echo $ rows-&gt; user_id?&gt;  ;“&gt; 
&lt;?php echo ucfirst($ rows-&gt; first_name)?&gt; 
&lt; / option&gt; 
&lt;?php 
} 
?&gt; 
&lt; / select&gt;  ; 
&lt; / div&gt; 
  code>  pre> 
  div>

make select-box array instead of single value.

<select id="chkveg""  class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >

than you need to ALTER your column datatype to varchar or text.

after that you can implode function to create string value like

'user_id'=> implode(',',$this->input->post('user_id')); //eg.1,2,3

Reference link : http://www.w3schools.com/php/func_string_implode.asp

You should use 'user_id'=>implode(',',$this->input->post('user_id')); to convert your array into comma separated value. And after that you can store it into database table. Because you can not directly stores the array into database table