Welcome Guest, Not a member yet? Register   Sign In
How to add a custom db call to codeigniter
#1

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);
   }
}
Reply
#2

There is already an "insert_batch" method that might be what you're looking for. 

You can always create a MY_Model class, though, that extends CI_Model and add that method to it.
Reply
#3

As killishan said there is a method described here:

http://www.codeigniter.com/userguide3/da...sert-batch

Reply




Theme © iAndrew 2016 - Forum software by © MyBB