php CodeIgniter insert_batch()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27206178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:14:20  来源:igfitidea点击:

CodeIgniter insert_batch()

phpcodeigniter

提问by user3827056

OK i am trying to figure how to use insert_batch

好的,我想弄清楚如何使用 insert_batch

I am tring something like this

我正在尝试这样的事情

function checkboxes($data,$category)
    {

        $insert=array(
                        'story'=>$data
                        'category'=>$category
                     );

        $this->db->insert_batch('stories_to_categories',$insert);
    }

For $data i have array, which could have range of values and keys

对于 $data 我有数组,它可以有值和键的范围

(
    [0] => 1
    [1] => 6
    [2] => 14
    [3] => 15
    [4] => 18
)

For category i will have only one value for example 2

对于类别,我将只有一个值,例如 2

I try to achive in my tabele

我试图在我的表格中取得成就

  story category
------------------------
    1        2
    6        2
    14       2
    15       2
    18       2

Could someone help me i am in such a pain!

有人能帮我吗,我很痛苦!

回答by linil

You can achieve this by making a little modification to your code. CI documentation shows that batch insert expects an array that is embedded with associative arrays: 1 associative array for each new row to be inserted, mapping the column to the value.

您可以通过对代码进行一些修改来实现这一点。CI 文档显示批量插入需要一个嵌入关联数组的数组:每个新行插入 1 个关联数组,将列映射到值。

Practically, you would want to build an array like this for your $insert:

实际上,您可能希望为您的 构建一个这样的数组$insert

$insert=array(
    array('story'=>1, 'category'=>2).
    array('story'=>6, 'category'=>2).
    array('story'=>14, 'category'=>2).
    array('story'=>15, 'category'=>2).
    array('story'=>18, 'category'=>2).
);

Since your category is constant, you might want to use a function:

由于您的类别是常数,您可能需要使用一个函数:

function _insert_($data, $category='2', $options=array('data'=>'story', 'category'=>'category'))
{
    $return = array();
    foreach ($data as $value)
    {
        $return[] = array($options['data']=>$value, $options['category']=>$category);
    }
    return $return;
}

You can then have something like the following:

然后你可以有如下内容:

$this->db->insert_batch('stories_to_categories',_insert_($data));

Hope this helps.

希望这可以帮助。

Find Reference(s) below:

在下面找到参考:

See CodeIgniter reference here: CodeIgniter Active Record: #Insert

请参阅此处的 CodeIgniter 参考:CodeIgniter Active Record: #Insert

edit: Codeigniter 3.0 Query Builder Class: inserting data

编辑:Codeigniter 3.0 查询生成器类:插入数据

回答by user3827056

function checkboxes($category_checkboxes,$last_story_id)
    {
        foreach ($category_checkboxes as $box) 
        {
            $insert[] = array(
                              "category" => $box,
                              "story" => $last_story_id
                             );

        }

        $this->db->insert_batch('stories_to_categories',$insert);
    }