Guys, I would like to add my custom "multiple insert" function to CodeIgniter but I'm not sure what would be the best place for it. This is what the function looks like:
Code:
function insert_multiple($table_name, $data)
{
$db = sitedb('','PDO');
$sub_data = array_chunk($data, 1000);
for ($b = 0; $b < count($sub_data); $b++)
{
$insert_values = array();
for ($a = 0; $a < count($sub_data[$b]); $a++)
{
$insert_values = array_merge($insert_values, array_values($sub_data[$b][$a]));
$placeholder[] = '(' . implode(', ', array_fill(0, count($sub_data[$b][$a]), '?')) . ')';
}
$sql2 = "INSERT INTO $table_name (" . implode(",", array_keys($sub_data[$b][0])) . ") VALUES " . implode(',', $placeholder) . "";
$prepare = $db->prepare($sql2);
try
{
$prepare->execute($insert_values);
}
catch (PDOException $e)
{
echo "<pre>";
print_r($sub_data[$b]);
echo "</pre>";
echo $e->getMessage();
print_r($db->errorInfo());
}
unset($insert_values);
unset($placeholder);
}
}