繁体   English   中英

Codeigniter 插入批处理数组

[英]Codeigniter Insert Batch array

我正在使用 Codeigniter 将多表单输入数据插入到数据库中。 我有这个帖子输入数组:

 Array
(
 [subject_id] => Array
    (
        [0] => 1
        [1] => 1
    )

[question] => Array

    (
        [0] => test
        [1] => test2
    )

[option1] => Array
    (
        [0] => test
        [1] => test2
    ) )

我不明白如何将此数组转换为插入 如何使用插入批处理插入此数组。

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

这是我用于发布数据的表单代码:

                <form method="post">
            <input type="text" name="subject_id[]" >
            <input type="text" name="question[]" >
            <input type="text" name="record[]" >

            // Down side Part is appended when user want to add more question

            <input type="text" name="subject_id[]" >
            <input type="text" name="question[]" >
            <input type="text" name="record[]" >

            <input type="submit" name="submit" >
            </form>

下面是我想要的数组格式。

$data = array(
            array(
                'subject_id' => 'My title' ,
                'question' => 'My Name' ,
                'option1' => 'My date'
                ),
            array(
                'subject_id' => 'Another title' ,
                'question' => 'Another Name' ,
                'option1' => 'Another date'
                )
            );
<?php
    $i = 0;
    foreach($subject_id as $key=>$val)
    {
          $data[$i]['subject_id'] = $val;
          $data[$i]['question'] = $question[$key];
          $data[$i]['option1'] = $record[$key];
          $i++;
    }
    $this->db->insert_batch('mytable', $data);

?>

尝试如下:假设$records是您要插入的数组。

foreach ($records as $record) 
{

    for ($i=0; $i < count($record); $i++) 
    {   
    $data[$i]['subject_id'] = $record['subject_id'][$i];
    $data[$i]['question'] = $record['question'][$i]
    $data[$i]['option1'] = $record['option1'][$i];
    }

}

然后

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM