codeIgniter:INSERT无周期的多个记录 [英] CodeIgniter: INSERT multiple records without cycle

查看:154
本文介绍了codeIgniter:INSERT无周期的多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的使用codeIgniter活动记录多个INSERT记录而不为,的foreach等?

我目前的code:

 的foreach($标记为$标签){
    $标记=修剪($标签);
    $数据=阵列(
        'topic_id'=> $ topic_id,
        USER_ID'=> $ user_ID的,
        文本=> $标签
    );
    $这个 - > DB->插入('topic_tags',$数据);
}
 

解决方案

codeigniter活动记录有一个函数 insert_batch 我认为这是你所需要的。

  $数据=阵列(
   阵列(
      '标题'=> 我的冠军,
      '名'=> '我的名字' ,
      日期=> 我的时间
   ),
   阵列(
      '标题'=> 另一个称号,
      '名'=> 别称,
      日期=> 另一个日期
   )
);

$这个 - > DB-> insert_batch('mytable的',$数据);

//生成:INSERT INTO mytable的(标题,姓名,日期)VALUES('我的题目,我的名字,我的时间),(另一个称号,另一名,另一个日期)
 

<一个href="http://ellislab.com/$c$cigniter/user-guide/database/active_record.html#insert">http://ellislab.com/$c$cigniter/user-guide/database/active_record.html#insert

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

My current code:

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 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')

http://ellislab.com/codeigniter/user-guide/database/active_record.html#insert

这篇关于codeIgniter:INSERT无周期的多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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