2017-02-07 7 views
0

私は自分のページに1つの要件フォームを持っています。そのフォームでは、選択ボックスでユーザーをドロップダウンしています。一度に複数のユーザーを選択し、選択したユーザーをデータベースに保存する必要があります。複数のselect box値をcodeigniterのデータベースに格納する方法はありますか?

コントローラー:

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

モデル:あなたはカンマ区切りにあなたの配列を変換するために'user_id'=>implode(',',$this->input->post('user_id'));を使用する必要があります

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

どのところであなたはフォームを提出されています

そのあとは

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

参考リンクのような文字列値を作成する機能を内破することができますか? AJAXまたはPHPで? –

+0

私はPHPのみを使用してフォームを送信していました – M5533

答えて

1

単一値の代わりに選択ボックス配列を作成します。

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

カラムのデータ型をvarcharまたはtextに変更する必要があります。 http://www.w3schools.com/php/func_string_implode.asp

0

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

ビューページ。その後、データベーステーブルに格納することができます。配列をデータベーステーブルに直接格納することはできないため、

関連する問題