0
votes

I have multiple array to insert into database but i don't fix the field name because can select format table data and insert into database but can check field name with $id_template.

This my format table(example) enter image description here

So i want to know how can i get data from multiple array to insert into database

This my code in controller

    $column = $this->m_rate_template->get_column($id_template);
    $colum_detail = implode(",", $column);
    $column_cut = explode(",", $colum_detail);  //example data get format is Array ( [0] => min [1] => max)
    foreach ($column_cut as $key => $val){

        $a = $this->input->post($column_cut[$key]);
            foreach ($a as $key1 => $val1){
                echo $val1;
                $child_data = array(
                        'id' => $this->m_rate_template->generate_id_in_template($template_name),
                        'id_rate' => $id_rate,
                        $column_cut[$key] => $val1
                );
                $this->m_rate_template->insert_rate($child_data, $template_name);
            }
    }
3

3 Answers

1
votes

My data it show like this

Array ( [id] => 4ae665037e [id_rate] => 7f881e02bb [min] => 1 ) 
Array ( [id] => bc3e60157f [id_rate] => 7f881e02bb [min] => 2 )
Array ( [id] => 082de3ad82 [id_rate] => 7f881e02bb [max] => 1 ) 
Array ( [id] => ee135ecd8a [id_rate] => 7f881e02bb [max] => 2 ) 

actually, data should be like this

 Array ( [id] => 4ae665037e [id_rate] => 7f881e02bb [min] => 1 [max] => 2) 
 Array ( [id] => 082de3ad82 [id_rate] => 7f881e02bb [max] => 1 [max] => 2) 

Update

$array = array(
 [0] => array(
  'min' => '2500',
  'max' => '5000'
 ),
[1] => array(
 'min' => '5001',
 'max' => '7000'
)

)

1
votes

You can use batch insert to insert multiple

$this->db->insert_batch();

first parameter is table name and second is array of arrays(records)

0
votes

if you have want to insert multiple record in table then you can also use codeigniter inbuilt insert_batch function without make query in loop. so i thing your execution will be fast.

you have want to array in below format.

$array = array(
   [0] => array(
    'column 1' => 'value 1',
    'column 2' => 'value 1'
   ),
  [1] => array(
    'column 1' => 'value 2',
    'column 2' => 'value 2'
   )
)

$this->db->insert_batch('tbl_name',$array)

so please make your code and generate your array as above in loop and simply pass your array in insert_batch function.