2015-04-24 40 views
0

我想在點擊數據庫中插入多行,如果行被選中使用多個複選框 enter image description here在數據庫codeigniter中插入多個值?

這裏是我的代碼=> 1)控制器:guard.php (在這裏,我以學生的名單的陣列編號並將它們逐一傳遞給另一個函數get_leave_data,該函數使用學生的ID並從另一個表leave_application返回有關該學生的更多信息。

public function students_out(){ 
    $request=$this->input->post('approve_leave_status'); 
    if($request=="OUT"){ 
    $check_list_out[]=$_POST['check_list_out']; 
    if(!empty($check_list_out)){ 
      foreach ($check_list_out as $check_list_id) { 
      $student_data=$this->get_leave_data($check_list_id); 
      $this->insert_student_outside($student_data); 
      } 
      $this->load->helper('url'); 
      $this->load->view('view_guard'); 
    } 
}else{ 
    $this->load->helper('url'); 
    $this->load->view('view_guard'); 
} 

} ` 

public function get_leave_data($id){ 
$this->load->model('model_guard'); 
$data=$this->model_guard->get_data($id); 
return $data; 
} 

public function insert_student_outside($std_data){ 
    $this->load->model('model_guard'); 
    $data=$this->model_guard->insert_student_out($std_data); 
return $data; 
} 

2)型號:model_guard.php (功能get_data()get_data2()回報更多的信息有關學生和功能insert_student_out()插入學生對student_outside table

public function get_data($id){ 
    $this->db->select('leave_id,leave_from_roll_no,leave_student_name,leave_going_to,leave_from_date,leave_till_date,leave_hostel_no,leave_status'); 
    $this->db->from('leave_application'); 
    $this->db->where('leave_id',$id[0]); 
    $query=$this->db->get(); 
    $data1=$query->result(); 
    $data2=$this->get_data2($data1); 
    $final_array=array_merge($data1,$data2); 
    return $final_array; 
    } 

    public function get_data2($array){ 
    foreach ($array as $key) { 
    $roll_no=$key->leave_from_roll_no; 
    } 
    $this->db->select('student_year,student_semester,student_parent_email'); 
    $this->db->from('students'); 
    $this->db->where('student_roll_no',$roll_no); 
    $query=$this->db->get(); 
    $data=$query->result(); 
    return $data; 
    } 

    public function insert_student_out($std_data){ 
    $roll_no=$std_data[0]->leave_from_roll_no; 
    $id=$std_data[0]->leave_id; 
    $date_out=date('Y-m-d'); 
    $inside_date=NULL; 
    $date_allowed=$std_data[0]->leave_till_date; 
    $array=array(
    'outside_id'=>$id, 
    'outside_roll_no'=>$roll_no, 
    'outside_date_out'=>$date_out, 
    'outside_date_in'=>$inside_date, 
    'outside_date_allowed'=>$date_allowed 
    ); 
    if($this->db->insert('students_outside',$array)){ 
     return true; 
    }else{ 
     false; 
    } 


    } 
+0

我沒有看到你請在您的控制中調用您的模型文件 – Saty

+0

實際問題是什麼。 –

+0

根據@saty你應該加載你的模型,然後從你的控制器調用它。 –

回答

0

您嘗試插入batch_array使用codeignator

使用batch_array的正確語法是: -

$array=array(
    'coloum_name'=>$id, 
    'coloum_name'=>$roll_no, 
    'coloum_name'=>$date_out, 
    'coloum_name'=>$inside_date, 
    'coloum_name'=>$date_allowed 
    ); 

if($this->db->insert_batch('students_outside',$array)){ 
     return true; 
    }else{ 
     false; 
    } 

你可以閱讀手冊insert_batch這裏

+0

我m到處使用insert_batch 嚴重錯誤:警告 消息:array_keys()預計參數1是數組,字符串給出 文件名:數據庫/ DB_active_rec.php 行號:1114 –

+0

@electron我有編輯自己的帖子列名稱是您的表列名稱 – Saty

+0

它也不起作用 –

0

好吧,我假設在$std_data你有你的模型insert_student_out()功能不止一個record.then嘗試這個 -

public function insert_student_out($std_data){ 

foreach($std_data as $row){ 
     $roll_no=$row->leave_from_roll_no; 
     $id=$row->leave_id; 
     $date_out=date('Y-m-d'); 
     $inside_date=NULL; 
     $date_allowed=$row->leave_till_date; 
     $array=array(
       'outside_id'=>$id, 
       'outside_roll_no'=>$roll_no, 
       'outside_date_out'=>$date_out, 
       'outside_date_in'=>$inside_date, 
       'outside_date_allowed'=>$date_allowed); 

     $this->db->insert('students_outside',$array); 

    } 
} 
+0

$ std_data是一個對象而不是對象數組 –

相關問題