![]() |
$this->db->insert Within Loop - 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: $this->db->insert Within Loop (/showthread.php?tid=15951) |
$this->db->insert Within Loop - El Forum - 02-19-2009 [eluser]Jay Logan[/eluser] Why doesn't this work? I don't get an error. But only 1 entry is inserted into the database. I did a print_r on the $events variable and there are about 25 items in it. I'm so confused. Code: function copy_events_for_template($meet_id, $template_id, $host_id) $this->db->insert Within Loop - El Forum - 02-20-2009 [eluser]minhbu[/eluser] try this code foreach ($events as $k => $event) { $data['event_id'] = $event['event_id']; $data['gender'] = $event['gender']; $data['template_id'] = $template_id; $this->db->insert('meet_template_events', $data); } $this->db->insert Within Loop - El Forum - 02-20-2009 [eluser]TheFuzzy0ne[/eluser] I'm guessing you are getting an error, but you can't see it as you've turned down the error reporting level. Can you confirm that this isn't the case? I've tidied up your code a little. It may not make any difference, but it only took a few seconds, so I figured I should do it: Code: function copy_events_for_template($meet_id, $template_id, $host_id) $this->db->insert Within Loop - El Forum - 02-20-2009 [eluser]xwero[/eluser] Instead of inserting each row individually you better create a string of values. Code: $this->db->where('meet_id', $meet_id); If your database is capable of better ways to query it, don't be afraid to drop the nice CI syntax. $this->db->insert Within Loop - El Forum - 02-20-2009 [eluser]Jay Logan[/eluser] Fixed. Silly me didn't notice the table had unique index key on the template_id field. Thanks. |