0

Hi I faced a problem when I insert some value from dynamic adding table then it insert with cycle.

But I want to insert all my arrays at once how it possible.

This is my Model:

$id=$this->input->post('id');
$rank=$this->input->post('rank');
$name=$this->input->post('name');
$mob=$this->input->post('mob');
$count = count($this->input->post('id'));

$data = array();

for($i=0, $i>$count; $i++) {

    $data[] = array(
    'id'=> null,
    'pers_no' => $id[$i],
    'rank' => $rank[$i],
    'name' => $name[$i],
    'mobile' => $mob[$i],
    'vendor_id' => $vendor[$i],
    );

    $result = $this->db->insert_batch('workers_tbl', $data);
}
Forkan Akon
  • 31
  • 1
  • 7

4 Answers4

2
$id=$this->input->post('id');
$rank=$this->input->post('rank');
$name=$this->input->post('name');
$mob=$this->input->post('mob');
$count = count($this->input->post('id'));

$data = array();

for($i=0, $i>$count; $i++) {

$data[] = array(
'id'=> null,
'pers_no' => $id[$i],
'rank' => $rank[$i],
'name' => $name[$i],
'mobile' => $mob[$i],
'vendor_id' => $vendor[$i],
);}

$result = $this->db->insert_batch('workers_tbl', $data);  

Please try this may be work for you....

Put your insert_batch query outside the loop completed. i have only once change and remain same..

Ankur Radadiya
  • 245
  • 3
  • 11
1

You are inserting one record, this isn't called batch insertion. Just create an array and insert the record. In your model function

public function insert_record($data)
{
   $record=array(
     'pers_no' => $data['id'],
     'rank' => $data['rank'],
     'name' => $data['name'],
     'mobile' => $data['mobile'],
   );
   $this->db->insert('workers_tbl',$record);
}

You can secure your input by doing

$this->db->escape($data); // before creating the record array

From your controller just call this function once you store the post in some variable like

public function insert_worker()
{
   if($_POST)
   {
     $data=$this->input->post();
     $this->model_name->insert_record($data);
   }
}

if you want to create one huge array of multiple records . Read this

Community
  • 1
  • 1
Mudassar Khani
  • 1,469
  • 1
  • 20
  • 39
0
$id=$this->input->post('id');
$rank=$this->input->post('rank');
$name=$this->input->post('name');
$mob=$this->input->post('mob');
$count = count($this->input->post('id'));

$data = array();

for($i=0, $i>$count; $i++) {

    $data[] = array(
    'pers_no' => $id[$i],
    'rank' => $rank[$i],
    'name' => $name[$i],
    'mobile' => $mob[$i],
    'vendor_id' => $vendor[$i],
    );


}
$result = $this->db->insert_batch('workers_tbl', $data);
Crustamet
  • 104
  • 1
  • 11
-1

Try this...

$count = $_POST['id'];

for($i=0, $i < $count; $i++){

$data[] = array(
    'pers_no' => $id[$i],
    'rank' => $rank[$i],
    'name' => $name[$i],
    'mobile' => $mob[$i],
    'vendor_id' => $vendor[$i]

);

}

$this->db->insert('workers_tbl',$data);

This may work. Thanks.

GYaN
  • 2,327
  • 4
  • 19
  • 39