简体   繁体   English

Codeigniter多维数组插入数据库

[英]Codeigniter multidimensional array insert into database

Hello im trying to insert a multidimensional array into mysql using codeigniter. 你好我试图使用codeigniter插入一个多维数组到mysql。 Im having some trouble getting the values to enter correctly. 我无法正确输入值。 Here is how the multidimensional array looks 这是多维数组的外观

Array ( [2] => Array ( [A] => FName 2 [B] => LName 2 [C] => PhoneNo 2 [D] => FaxNo 2 ) [3] => Array ( [A] => FName 3 [B] => LName 3 [C] => PhoneNo 3 [D] => FaxNo 3 ) [4] => Array ( [A] => FName 4 [B] => LName 4 [C] => PhoneNo 4 [D] => FaxNo 4 ) [5] => Array ( [A] => FName 5 [B] => LName 5 [C] => PhoneNo 5 [D] => FaxNo 5 ) [6] => Array ( [A] => FName 6 [B] => LName 6 [C] => PhoneNo 6 [D] => FaxNo 6 ) [7] => Array ( [A] => FName 7 [B] => LName 7 [C] => PhoneNo 7 [D] => FaxNo 7 ) [8] => Array ( [A] => FName 8 [B] => LName 8 [C] => PhoneNo 8 [D] => FaxNo 8 ) [9] => Array ( [A] => FName 9 [B] => LName 9 [C] => PhoneNo 9 [D] => FaxNo 9 ) ) 

Here is what i've tried doing 这是我尝试过的

function insertfiles($arr_data)
{
    foreach ($arr_data as $value) {
        foreach($value as $key => $a){
            $data = array(
                   'Firstname' => $a,
                   'Lastname' => $a,
                   'Phone'=>$a,
                   'Fax' =>$a
                );

                $this->db->insert('test', $data); 
        }
    }
}

Im sure im messing this up horribly, it is inserting the values into the database but it's inserting them as follows 我确定我搞砸了这个,它将值插入数据库,但它插入如下

since it wont let me upload an image im providing a link to view this image online. 因为它不会让我上传图像我提供链接在线查看此图像。 http://psadatadesign.com/img/test-bmp.jpg http://psadatadesign.com/img/test-bmp.jpg

any help would be appreciated. 任何帮助,将不胜感激。 As im trying to figure out what i have done wrong. 正如我试图弄清楚我做错了什么。

Use this rather your own function 使用它而不是你自己的功能

function insertfiles($arr_data)
{
    foreach($array_data as $a){
        $data = array(
               'Firstname' => $a['A'],
               'Lastname' => $a['B'],
               'Phone'=>$a['C'],
               'Fax' =>$a['D']
            );

            $this->db->insert('test', $data); 
    }
}

There are two cases for this one for case 1 案例1中有两种情况

If the array you pass does not contain a named key, check the function below 如果传递的数组不包含命名键,请检查以下函数

$table_array_index_key = array( //if your array does not have a named key.
    0 => array( //first row
        0 => 'first_name 1',
        1 => 'last_name 1',
        2 => 'phone 1',
        3 => 'fax 1'
        ),
    1 => array( //second row
        0 => 'first_name 2',
        1 => 'last_name 2',
        2 => 'phone 2',
        3 => 'fax 2'
        ),
    );

$row = array();
$columns = array();
for($x=0;$x<count($table_array_index_key);$x++){
        $row = array(
            'Firstname' => $table_array_index_key[$x][0], //$table_array[1][0] means table_array row 2 column 1  
            'Lastname' => $table_array_index_key[$x][1],
            'Phone' =>  $table_array_index_key[$x][2],
            'Fax' => $table_array_index_key[$x][3] 
        );
        array_push($columns,$row);
        $rows = array();
}

echo "<pre>";
print_r($columns);

else if your array contains named keys, check this out. 否则,如果您的数组包含命名键,请检查它。

$table_array_index_name = array( //if your array have a named key.
    0 => array( //first row
        'A' => 'first_name 1',
        'B' => 'last_name 1',
        'C' => 'phone 1',
        'D' => 'fax 1'
        ),
    1 => array( //second row
        'A' => 'first_name 2',
        'B' => 'last_name 2',
        'C' => 'phone 2',
        'D' => 'fax 2'
        ),
    );

$rows = array();
$columns = array();
$arrayNames = array('A','B','C','D'); 
for($x=0;$x<count($table_array_index_name);$x++){
        $row = array(
            'Firstname' => $table_array_index_name[$x][$arrayNames[0]], //note $arrayNames[0] = A
            'Lastname' => $table_array_index_name[$x][$arrayNames[1]],
            'Phone' =>  $table_array_index_name[$x][$arrayNames[2]],
            'Fax' => $table_array_index_name[$x][$arrayNames[3]] 
        );
        array_push($columns,$row);
        $rows = array();
}

echo "<pre>";
print_r($columns);

For which you can loop further like this one: 你可以像这样进一步循环:

$table_array_index_name = array( //if your array have a named key.
    0 => array( //first row
        'A' => 'first_name 1',
        'B' => 'last_name 1',
        'C' => 'phone 1',
        'D' => 'fax 1'
        ),
    1 => array( //second row
        'A' => 'first_name 2',
        'B' => 'last_name 2',
        'C' => 'phone 2',
        'D' => 'fax 2'
        ),
    );

$rows = array();
$columns = array();
$arrayNames = array('A','B','C','D');
$dbFieldName = array('Firstname','Lastname','Phone','Fax');
for($x=0;$x<count($table_array_index_name);$x++){
    for($y=0;$y<count($arrayNames);$y++){
        $row[$dbFieldName[$y]] = $table_array_index_name[$x][$arrayNames[$y]];
    }
        array_push($columns,$row);
        $rows = array();
}

echo "<pre>";
print_r($columns);

which will return the following data: 这将返回以下数据:

Array
(
    [0] => Array
        (
            [Firstname] => first_name 1
            [Lastname] => last_name 1
            [Phone] => phone 1
            [Fax] => fax 1
        )

    [1] => Array
        (
            [Firstname] => first_name 2
            [Lastname] => last_name 2
            [Phone] => phone 2
            [Fax] => fax 2
        )

)

Personally I prefer the for loop because we can have more freedom and control when manipulating arrays. 我个人更喜欢for循环,因为在操作数组时我们可以有更多的自由和控制。

For the $this->db->insert_batch function, please check out the code igniter active record documentation: https://ellislab.com/codeigniter/user-guide/database/active_record.html 对于$ this-> db-> insert_batch函数,请查看代码点火器活动记录文档: https ://ellislab.com/codeigniter/user-guide/database/active_record.html

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

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