How to Create Codeigniter Batch Insert Array

How to create Codeigniter batch insert array

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

Codeigniter Insert Batch array

<?php
$i = 0;
foreach($subject_id as $key=>$val)
{
$data[$i]['subject_id'] = $val;
$data[$i]['question'] = $question[$key];
$data[$i]['option1'] = $record[$key];
$i++;
}
$this->db->insert_batch('mytable', $data);

?>

Insert array batch in codeigniter

$post_array = array(
"notification_title"=>array("Hello! We have a good news for you.","Hello! We have a good news for you."),
"notification_message"=>array("Now you can choose up to three types of advertisers that you wish to collaborate.","Now you can choose up to three types of advertisers that you wish to collaborate."),
"notification_type"=>array('1','1'),
"notification_language"=>array('1','1')
);
$data = array();
$i = 0;
foreach($post_array as $key=>$val) {
$i = 0;
foreach($val as $k=>$v) {
$data[$i][$key] = $v;
$i++;
}
}
echo '<pre>';
print_r($data);

so you will get array like below and you can use it with insert batch

$data = array(
array(
'notification_title'=> 'Hello! We have a good news for you',
'notification_message'=> 'Now you can choose up to three types of advertisers that you wish to collaborate.',
'notification_type'=>'1',
'notification_language'=>'1'
),
array(
'notification_title'=> 'Hello! We have a good news for you',
'notification_message'=> 'Now you can choose up to three types of advertisers that you wish to collaborate.',
'notification_type'=>'1',
'notification_language'=>'1'
),
);

here key will be your column name of table and than

$this->db->insert_batch('mytable', $data); 

hope this will help you..

CodeIgniter insert_batch()

You can achieve this by making a little modification to your code.
CI documentation shows that batch insert expects an array that is embedded with associative arrays: 1 associative array for each new row to be inserted, mapping the column to the value.

Practically, you would want to build an array like this for your $insert:

$insert=array(
array('story'=>1, 'category'=>2).
array('story'=>6, 'category'=>2).
array('story'=>14, 'category'=>2).
array('story'=>15, 'category'=>2).
array('story'=>18, 'category'=>2).
);

Since your category is constant, you might want to use a function:

function _insert_($data, $category='2', $options=array('data'=>'story', 'category'=>'category'))
{
$return = array();
foreach ($data as $value)
{
$return[] = array($options['data']=>$value, $options['category']=>$category);
}
return $return;
}

You can then have something like the following:

$this->db->insert_batch('stories_to_categories',_insert_($data));

Hope this helps.

Find Reference(s) below:

See CodeIgniter reference here: CodeIgniter Active Record: #Insert

edit: Codeigniter 3.0 Query Builder Class: inserting data

How to create insert_batch array in Codeigniter

I am assuming you want to reorganize arrays. Try this:

$a = array(
'TraineeID' => array(
3001,
3002
),
'wDate' => array(
'123',
'234'
),
'Hour' => array(
12,
13
)
);

$keys = array_keys($a);//counting outer array

if(count($keys[0]) > 0)//checking if inner array has values at all
{
$new_a = [];//initializing expecting array

foreach($a[$keys[0]] as $k => $v)
{
for($i = 0; $i < count($keys); $i++)
{
$new_a[$k][$keys[$i]] = $a[$keys[$i]][$k];
}
}
}

//echo '<pre>', var_dump($new_a);

Array To String while inserting data using insert_batch in codeigniter

Try creating the array like this :

$insertArray = array();
foreach($add as $data){
$new_add = array(
'col1'=>$data['col1'],
'col2'=>$data['col2'],
'col3'=>$data['col3'],
'col4'=>$data['col4'],
'col5'=>$data['col5'],
'col6'=>$data['col6']
);
array_push($insertArray,$new_add);
}

And call the insert_batch like this :

$this->db->insert_batch('test', $insertArray); 

And please verify that "col1","col2"... are valid column name of table "test".



Related Topics



Leave a reply



Submit