0

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);
            }
    }

2 Answers 2

1

You can use batch insert to insert multiple

$this->db->insert_batch();

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Yes i got it, but problem is that i don't know about your looping data, i just simply suggest you if do you want to insert multiple array in database with codeigniter then please always try to use insert_batch() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.