CodeIgniter:无循环插入多条记录 [英] CodeIgniter: INSERT multiple records without cycle

查看:33
本文介绍了CodeIgniter:无循环插入多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 CodeIgniter Active Record 中使用多个 INSERT 记录而无需 for、foreach 等?

It's possible to use multiple INSERT records in CodeIgniter Active Record without for, foreach and etc. ?

我当前的代码:

foreach($tags as $tag) {
    $tag = trim($tag);
    $data = array(
        'topic_id'  =>  $topic_id,
        'user_id'   =>  $user_id,
        'text'      =>  $tag
    );
    $this->db->insert('topic_tags', $data);
}

推荐答案

Codeigniter 活动记录有一个功能 insert_batch 我认为这就是你需要的

Codeigniter active record has a function insert_batch i think that is what you need

$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); 

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

适用于 Codeigniter 3.x 和 Codeigniter 2.2.6

Works for both Codeigniter 3.x and Codeigniter 2.2.6

更新的链接

insert_batch() 用于 Codeigniter 3.x

用于 Codeigniter 2.x 的insert_batch()

这篇关于CodeIgniter:无循环插入多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆