Codeigniter插入批处理数组 [英] Codeigniter Insert Batch array

查看:53
本文介绍了Codeigniter插入批处理数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter将多种格式的输入数据插入数据库.我有这个帖子输入数组:

I am Inserting a multiple form input data in to database using Codeigniter. I have this post input array:

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

[question] => Array

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

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

我不知道如何将这个数组转换为插入方法.如何使用插入批处理插入这个数组.

I don't get that how do i convert this array to insert How to insert this array using Insert batch.

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

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

This is the form code which i use for posting the 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>

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

Below is the Array format which i want.

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

?>

这篇关于Codeigniter插入批处理数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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