how to insert multiple data using array in CodeIgniter
controller:
function insert_sitelabourers(){
if($this->input->post('Submit')){
$sitename=$this->input->post('sitename');
$role=$this->input->post('Role[]');
$empname=$this->input->post('emp_name[]');
for($i=0;$i<count($role);$i++){
$data=array(
'site_labourer_siteid'=>$sitename,
'site_labourer_emproleid'=>$role[$i],
'site_labourer_empname'=>$empname[$i],
'site_labourer_active'=>1
);
$sitelabourers[]=$data;
}
$data['msg']=$this->site_model->insert_sitelabourers($sitelabourers);
$data['title']="Raj";
$data['viewsite']=$this->site_model->view_site();
$this->load->view('layout/head',$data);
$this->load->view('site/sitelabourers',$data);
$this->load->view('layout/footer');
//print_r($sitelabourers);
}
}
view:
<td class="col-sm-4">
<select class="form-control" name="Role[]" id="rolename" onChange="getempname(this);" >
<option selected disabled>Select Role</option>
</select>
</td>
model:
function insert_sitelabourers($sitelabourers){
$query=$this->db->insert_batch('sitelabourers',$sitelabourers);
if ($query) {
return "<div class='alert alert-success'><strong class='alert-link'>Success!</strong> Site Labourers Inserted in Database.
</div>";
}else{
return "<div class='alert alert-danger'><strong>Danger!</strong>Site Labourers Inserted in Error.</div>";
}
}
function insert_sitelabourers(){
if($this->input->post('Submit')){
$sitename=$this->input->post('sitename');
$role=$this->input->post('Role[]');
$empname=$this->input->post('emp_name[]');
for($i=0;$i<count($role);$i++){
$data=array(
'site_labourer_siteid'=>$sitename,
'site_labourer_emproleid'=>$role[$i],
'site_labourer_empname'=>$empname[$i],
'site_labourer_active'=>1
);
$sitelabourers[]=$data;
}
$data['msg']=$this->site_model->insert_sitelabourers($sitelabourers);
$data['title']="Raj";
$data['viewsite']=$this->site_model->view_site();
$this->load->view('layout/head',$data);
$this->load->view('site/sitelabourers',$data);
$this->load->view('layout/footer');
//print_r($sitelabourers);
}
}
view:
<td class="col-sm-4">
<select class="form-control" name="Role[]" id="rolename" onChange="getempname(this);" >
<option selected disabled>Select Role</option>
</select>
</td>
model:
function insert_sitelabourers($sitelabourers){
$query=$this->db->insert_batch('sitelabourers',$sitelabourers);
if ($query) {
return "<div class='alert alert-success'><strong class='alert-link'>Success!</strong> Site Labourers Inserted in Database.
</div>";
}else{
return "<div class='alert alert-danger'><strong>Danger!</strong>Site Labourers Inserted in Error.</div>";
}
}
Comments
Post a Comment