1

我尝试使用 codeigniter 在我的 MySQL 表中插入数据。

首先,我从配置 XML 中检索列,我应该在其中插入数据,目标节点应该从另一个 XML 中返回插入值。

    foreach ($sql_xml as $children) {
        foreach ($children as $index => $child) {
            if ($child != 'feed_id') {
                $keys[] = $index;
                $into[] = $child; //this holds the table columns
            }
        }
    }

然后我检索每行要插入的多个值。

  $products = array();
        $data = array();             
        foreach ($target_xml as $feeds) {
            $feeds =  $this->xml2array($feeds); //SimpleXMLObject to array
            $columns = new RecursiveIteratorIterator(new  RecursiveArrayIterator($feeds)); //get all recursive iteration            
            foreach ($columns as $index=>$column) {   
                     if (in_array($index, $keys)) { //here I get the values on matching nodes
                        $data[] = $column;
                    }    
                } 
            $products[] = $data;// this are the datas which should be inserted
            }

这里应该是插入:我一直在寻找有一个很好的解决方法的文档,如果插入的是一个关联数组,在我的情况下,它是在匹配键的流上创建的。

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

问题是into包含目标列的数组,我不知道如何在我的产品数组中推送主题。

4

1 回答 1

2

我没有正确理解您的示例代码,但这是我的尝试。

你想要这个代码

if ($child != 'feed_id') {
     $keys[] = $index;
     $into[] = $child; //this holds the table columns
}

变成这样的东西

if ($child != 'feed_id') {
     $keys[$index] = $child;
}

你想要这个

if (in_array($index, $keys)) {
    $data[] = $column;
}

变成这样

if ( array_key_exists($index, $keys) ) {
    $data[$keys[$index]] = $column;
}
于 2013-08-23T21:16:04.923 回答