Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Codeigniter batch insert array

How can I create a CI batch insert array which is like

$data = array(    array(       'title' => 'My title' ,       'name' => 'My Name' ,       'date' => 'My date'    ),    array(       'title' => 'Another title' ,       'name' => 'Another Name' ,       'date' => 'Another date'    ) ); 

from my normal array which is

Array (     [0] => Array         (             [track_id] =>              [camp_id] => 1             [field_name] => email_title             [field_value] => sample         )      [1] => Array         (             [track_id] =>              [camp_id] => 1             [field_name] => email_date             [field_value] => 2013-07-02         )      [2] => Array         (             [track_id] =>              [camp_id] => 1             [field_name] => email_template             [field_value] => 2         ) ) 
like image 898
Happy Coder Avatar asked Jul 26 '13 07:07

Happy Coder


People also ask

How can add multiple data in CodeIgniter?

if you want to enter one data on a website then we can use the Insert Query Input method method and if you want to add multiple records to the database we will be able to use the insert_batch Query Method.

What is Insert_batch?

insert is for inserting one record and insert_batch is for inserting multiple records.

How do I batch insert in SQL?

Syntax to insert bulk data in MySQL Type the clause INSERT INTO and the table name in which you want to insert the data. Use the clause VALUES and then in the brackets write the data of the first row, close the brackets, and after the put the comma.

Where is active record in CodeIgniter?

Active Record ClassCodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.


1 Answers

Try this:

$data = array(    array(       'title' => 'My title' ,       'name' => 'My Name' ,       'date' => 'My date'    ),    array(       'title' => 'Another title' ,       'name' => 'Another Name' ,       'date' => 'Another date'    ) );  $this->db->insert_batch('mytable', $data);  

For more information, read here

like image 93
Erman Belegu Avatar answered Sep 22 '22 06:09

Erman Belegu