简体   繁体   中英

how to insert batch codeigniter

Ii everyone, I have an post data in array like this, I'm so confused how create the logic in controller:

POST Data:

Array
(
    [nama_agenda] => blalala
    [kilasan] => asdsadsadasd
    [tgl_agenda] => 2014-06-01
    [jam_agenda] => 13:27:30
    [komisi] => Array
    (
        [0] => 1
        [1] => 3
    )

    [fraksi] => Array
    (
        [0] => 1
        [1] => 4
    )

    [badan] => Array
    (
        [0] => 1
        [1] => 3
    )

    [anggota] => Array
    (
        [0] => 1
        [1] => 4
    )

    [bagian] => Array
    (
        [0] => 2
        [1] => 4
    )

)

My question is how to insert into database, in controller? Thank's for help. I'll appreciate.

get all the data in array using $this->input->post() eg:

$bagian= $this->input->post('bagian');

and create a array()

$arr=array(
'db_table_col_1'=>$bagian,
'db_table_col_2'=>$post_data,
'db_table_col_2'=>$post_data
);

pass this array to model

$this->your_model_name->function_name($arr);

then in model create function

function_name($arg){
$this->db->insert('table_name',$arr);
}

if you want to insert multiple row then just use foreach

<?php
$arr1=array();
$arr= array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array
    (
        '0' => 1,
        '1' => 3
    ),
'fraksi' => array
    (
        '0' => 1,
        '1' => 4
    ),
 'badan' => array
    (
        '0' => 1,
        '1' => 3
    ),
'anggota' => array
    (
        '0' => 1,
        '1' => 4
    ),
  'bagian' => array
    (
        '0' => 2,
        '1' => 4
    )
   );
  foreach($arr as $row){
if(is_array($row)){
array_push($arr1,$row);
}
  }
 print_r($arr1);

and then pass this array to batch_insert

function_name($arr1){
$this->db->insert_batch('table_name',$arr1); 
}

note arr1 syntax must be

 $arr1 = array(
 array(
  'table_col1' => 'My title' ,
  'table_col2' => 'My Name' 
  ),
 array(
  'table_col1' => 'other title' ,
  'table_col2' => 'other Name' 
  )

 );

?>

Since your structure is not well formed for insert_batch method. Your need to restructure it first. Consider this example:

$original_values = array(
    'nama_agenda' => 'blalala',
    'kilasan' => 'asdsadsadasd',
    'tgl_agenda' => '2014-06-01',
    'jam_agenda' => '13:27:30',
    'komisi' => array(1, 3),
    'fraksi' => array(1, 4),
    'badan' => array(1, 3),
    'anggota' => array(1, 4),
    'bagian' => array(2, 4),
);

$new_values = array();
for($x = 0, $size = count($original_values['komisi']); $x < $size; $x++) {
    foreach($original_values as $key => &$value) {
        if(!is_array($value)) {
            $new_values[$x][$key] = $value;
        } else {
            $new_values[$x][$key] = array_shift($value);
        }
    }
}

echo '<pre>';
print_r($new_values);

Should yield something like:

Array
(
    [0] => Array
        (
            [nama_agenda] => blalala
            [kilasan] => asdsadsadasd
            [tgl_agenda] => 2014-06-01
            [jam_agenda] => 13:27:30
            [komisi] => 1
            [fraksi] => 1
            [badan] => 1
            [anggota] => 1
            [bagian] => 2
        )

    [1] => Array
        (
            [nama_agenda] => blalala
            [kilasan] => asdsadsadasd
            [tgl_agenda] => 2014-06-01
            [jam_agenda] => 13:27:30
            [komisi] => 3
            [fraksi] => 4
            [badan] => 3
            [anggota] => 4
            [bagian] => 4
        )

)

Now you can use insert_batch() method.

$this->db->insert_batch('table_name', $new_values);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM