CodeIgniter Forums
Active Record or SQL Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record or SQL Question (/showthread.php?tid=12585)



Active Record or SQL Question - El Forum - 10-24-2008

[eluser]bennyhill[/eluser]
Currently I am inserting multiple records at once with this code:

Code:
$data = array();
        foreach ($_POST['tableGuests'] as $key => $value)
        {
            $temp = '('.$value.','.$_POST['owner_id'].')';
            array_push($data, $temp);
        }
        
       $values = implode(",",array_values($data));
       $sql = "INSERT INTO `table` (`attendee_id`, `owner_id`) VALUES ".$values;
       $this->db->query($sql);

Is there a better, more simple way to do it with Active Record?


Active Record or SQL Question - El Forum - 10-24-2008

[eluser]GSV Sleeper Service[/eluser]
I'm not sure if you can even do multiple inserts using Active Record, the docs don't mention anything about it.
I do like your implode method though, I can't believe I never thought of that before. I always used to do something like
Code:
$sep = "";
$sql = "";
foreach($thing as $k => $v){
    $sql.= $sep."('".$k."','".$v."')";
    $sep = ",";
}