duandang2838 2015-03-22 14:04
浏览 75
已采纳

Codeigniter多维数组插入数据库

Hello im trying to insert a multidimensional array into mysql using codeigniter. 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

any help would be appreciated. As im trying to figure out what i have done wrong.

  • 写回答

2条回答 默认 最新

  • dsxcv5652 2015-03-22 16:00
    关注

    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); 
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)