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 ) )
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.
insert is for inserting one record and insert_batch is for inserting multiple records.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With