首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >codeigniter将多维数组插入数据库

codeigniter将多维数组插入数据库
EN

Stack Overflow用户
提问于 2014-09-30 14:35:32
回答 1查看 1.6K关注 0票数 2

你好,我有这个多维数组,需要插入到数据库表中。到目前为止,我已经想出了这个解决方案,但并不完全有效。我有一个支持动态字段的表单,许多动态字段。我想用这个函数保存那些动态创建的字段。首先,我已经能够创建一个循环来格式化数据。我使用这个循环来格式化数据

代码语言:javascript
运行
复制
        $url = str_replace(' ','-',strtolower($this->input->post('title')));
        $data = ['business' => [
                                'title' => $this->input->post('title'),
                                'URL' => $url,
                                'category' => $this->input->post('category'),
                                'region' => $this->input->post('region')
                                ],
                 'description_' => [
                                'text' => $this->input->post('description'),
                                ],
                 'logo_' => [
                                'blob' => $this->input->post('logo'),
                                ]];
        $nyingi = [ 'location_' => ['text' => 'loc_txt','priority' => 'loc_order'], 
                    'photo_' => ['path' => 'p_txt'], 
                    'video_' => ['path' => 'v_txt'], 
                    'product_' => ['p_id' => 'pt_q', 'text' => 'pt_txt', 'expire' => 'pt_xpr', 'title' => 'pt_tt'], 
                    'service_' => ['p_id' => 'st_q', 'text' => 'st_txt', 'expire' => 'st_xpr', 'title' => 'st_tt'], 
                    'hours_' => ['time1' => 't1', 'time2' => 't2', 'type' => 'tt'], 
                    'map_' => ['text' => 'm_txt']
                ];
        foreach($nyingi as $ins => $vl){
            foreach($vl as $fld => $box){
                $uwazi = $this->input->post($box);
                if(is_array($uwazi) && 1<count($uwazi)){
                        foreach($uwazi as $bb){
                            $one[$ins][$fld][] = $bb;
                        }

                } elseif(is_array($uwazi) && 1==count($uwazi)) {
                    print_r($uwazi);
                }
            }
        }
         for($g=0;$g<count($one);$g++){
            for($h=0;$h<count($one[$g]);$h++){
                for($z=0;$z<count($one[$g][$h]);$z++){
                    //if(array_key_exists($one[$g][$h+1], $one)){
                        print $one[$g][$h][$z+1];
                    //}
                }
            }
        }
      $data = array_merge($data, $one);

上述代码的输出

代码语言:javascript
运行
复制
[location_] => Array
    (
        [text] => Array
            (
                [0] => Lemara Main Office
                [1] => Themi branch
                [2] => Sinoni branch
            )
        [priority] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )

    )

[photo_] => Array
    (
        [path] => Array
            (
                [0] => lemaraphoto.png
                [1] => themiphoto.png
                [2] => sinoniphoto.png
            )
    )
[video_] => Array
    (
        [path] => Array
            (
                [0] => lemaravideo.mp4
                [1] => themivideo.mp4
                [2] => sinonivideo.mp4
            )

    )

[product_] => Array
    (
        [p_id] => Array
            (
                [0] => product photo
                [1] => product 3 photo
                [2] => Product 2 photo
            )
        [text] => Array
            (
                [0] => product desc
                [1] => product 3 desc
                [2] => product 2 desc
            )

        [expire] => Array
            (
                [0] => product expire
                [1] => product 3 expire
                [2] => Product 2 expire
            )
        [title] => Array
            (
                [0] => product
                [1] => Product 3
                [2] => Product 2
            )

    )

[service_] => Array
    (
        [p_id] => Array
            (
                [0] => Service 2 photo
                [1] => service 3 photo
                [2] => service photo
            )
        [text] => Array
            (
                [0] => service 2 desc
                [1] => service 3 desc
                [2] => service desc
            )

        [expire] => Array
            (
                [0] => Service 2 expire
                [1] => service 3 expire
                [2] => service expire
            )
        [title] => Array
            (
                [0] => Service 2
                [1] => service 3
                [2] => service
            )

    )

)

我有这个add_bz函数

代码语言:javascript
运行
复制
function add_bz($data){
  $this->db->trans_start(); $er=0;
  foreach($data as $table => $sql){
   if($table==="business"){
    $this->db->insert($table, $sql);
    $id = $this->db->insert_id();
   } else {
   array_merge($sql, array('idd' => $id));
   $this->db->insert($table, $sql);}
  }
  $this->db->trans_complete();
  if ($this->db->trans_status() === FALSE){print "Transaction Fails";return FALSE;}
  return TRUE;
}

该函数只允许我以这种格式插入数据。

代码语言:javascript
运行
复制
[[business] => Array
    (
        [title] => Email Marketing
        [URL] => email-marketing
        [category] => 3
        [region] => 2
    )

[description_] => Array
    (
        [text] => Some desc
    )
[logo_] => Array
    (
        [blob] => mainlogo.png
    )
]

作为业务,decription_、logo_是db表,其中标题、URL、类别、区域、文本、blob是相应表的db列,其余是去往这些列的数据。

我希望它是如何工作的。例如在location_中

我希望将数组更改为我的工作格式或其他类似的工作格式

代码语言:javascript
运行
复制
location_ => ['text' => [[0] => 'Lemara Main Office'], 
              'priority' => [[0] => '1']
             ]
location_ => ['text' => [[1] => 'Themi Office'], 
              'priority' => [[1] => '2']
             ]
location_ => ['text' => [[2] => 'Sinoni Office'], 
              'priority' => [[2] => '3']
             ]

或者像这样

代码语言:javascript
运行
复制
location_ => [0 => [['text' => 'Lemara Main Office'], 
                    ['priority' => '1']
                   ],
             1 => [['text' => 'Themi Main Office'], 
                   ['priority' => '2']
                   ],
             2 => [['text' => 'Sinoni Main Office'], 
                   ['priority' => '3']
                   ]
             ],

并且还支持许多列,而不仅仅是这两个列。任何帮助我都深表感谢,因为我已经努力了几天才能做到这一点。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-10-11 18:52:19

我建议您将数组转换为JSON字符串,然后将其作为文本插入到DB中。JSON可以很好地处理你的多维数据,

将数组转换为JSON:http://php.net/manual/en/function.json-encode.php将JSON转换为array:http://php.net/manual/en/function.json-decode.php

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26114230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档