繁体   English   中英

使用codeigniter将特定的XML数据插入mysql

[英]insert specific XML data to mysql using codeigniter

我有一个xml文件,我想用两个新行将两个不同人的数据Payername 1和payername 2插入MySQL。 现在,当我使用我的代码时,我只获得了第一个PAYERNAME 1的archive_id,rf_reference,没有得到Payername 2,我尝试使用forloops但我的代码崩溃了。 这是XML FILE,但我是从另一个表的数据库中获取的,但这不是问题。 这是我编写的代码,这是数据库结构

public function decode(){  

      $xml_file = $this->db->select('xml_file')->order_by('id','desc')->limit(1)->get('transfer_packet')->row('xml_file');

      $xml = simplexml_load_string($xml_file);
     //  $Ref    = (String) $xml -> BkToCstmrDbtCdtNtfctn -> Ntfctn -> Acct -> Id -> Ref; // get BIC value

      /* use this when actual reference number is there in the XML file */

      $Ref =(String) $xml -> BkToCstmrDbtCdtNtfctn -> Ntfctn -> Ntry -> NtryDtls -> TxDtls -> RmtInf -> Strd -> CdtrRefInf -> Ref;


      $archive_id = (String) $xml ->BkToCstmrDbtCdtNtfctn ->Ntfctn -> Ntry -> NtryDtls -> TxDtls ->Refs -> AcctSvcrRef; // get archive id

      $orig_id = $this->db->select('payor_orig_id')->where('rf_reference',$Ref)->get('invoice')->row('payor_orig_id');



      /*Use this if there is more sums to calculate */

      $sum = 0.0;
       foreach ($xml -> BkToCstmrDbtCdtNtfctn -> Ntfctn -> TxsSummry -> TtlNtries as $value) {
       $sum += (float) $value -> Sum;
      } 




 $data = array(
 'currency'             =>'1',
 'payor_orig_id'     => $orig_id,
'allocated_amount'    =>'0' ,
'rf_reference'               => $Ref,
'payment_amount' => $sum,
'status'            =>  '1',
'payment_date' => date('Y-m-d'),
'received_date' => date('Y-m-d'),
'archive_id' => $archive_id,

);
$this -> db -> insert('payment', $data); // inserting a new row to our table

$id = $this -> db -> insert_id(); // get last inserted id

$uptArray   =   array('ORIG_ID' => $id);
$this -> db -> where('id', $id);
$this -> db -> update('payment', $uptArray); // updating ORIG_ID column 

$row = $this->db->query("SELECT * FROM transfer_packet ORDER BY id DESC     LIMIT 1;")->result("array");
$data = array(
                'status' => 2
             );
$id = (String) $row[0]['ID'];
$this->db->update('transfer_packet', $data, "ID = " . $id );
        }

我需要做的是,获取PAYERNAME 1和2的引用和存档ID,并将其放入数据库中。 我正在使用代码Igniter框架。

您必须遍历所有条目,然后遍历所有条目详细信息。 大约这样(未经测试):

<?php
public function decode(){  

    $xml_file = $this->db->
        select('xml_file')->
        order_by('id','desc')->
        limit(1)->
        get('transfer_packet')->
        row('xml_file');

    $xml = simplexml_load_string($xml_file);

    foreach($xml->BkToCstmrDbtCdtNtfctn->Ntfctn->Ntry as $entry) {
        foreach($entry->NtryDtls->TxDtls as $details) {
            // reference number
            $Ref = (String)$details->RmtInf->Strd->CdtrRefInf->Ref;

            // get archive id
            $archive_id = (String)$details->Refs->AcctSvcrRef; 

            $orig_id = $this->db->
                select('payor_orig_id')->
                where('rf_reference', $Ref)->
                get('invoice')->
                row('payor_orig_id');

            /*Use this if there is more sums to calculate */
            $sum = 0.0;
            foreach ($xml->BkToCstmrDbtCdtNtfctn->Ntfctn->TxsSummry->TtlNtries as $value) {
                $sum += (float)$value->Sum;
            } 

            $data = array(
                'currency'          => '1',
                'payor_orig_id'     => $orig_id,
                'allocated_amount'  => '0' ,
                'rf_reference'      => $Ref,
                'payment_amount'     => $sum,
                'status'            => '1',
                'payment_date'         => date('Y-m-d'),
                'received_date'     => date('Y-m-d'),
                'archive_id'         => $archive_id,
            );

            $this->db->insert('payment', $data); // inserting a new row to our table

            // set ORIG_ID equal to primary key
            $id = $this->db->insert_id(); // get last inserted id
            $uptArray = array('ORIG_ID' => $id);
            $this->db->
                update('payment', $uptArray)->
                where('id', $id); // updating ORIG_ID column 

            $row = $this->db->
                query("SELECT * FROM transfer_packet ORDER BY id DESC LIMIT 1;")->
                result("array");
            $data = array('status' => 2);
            $id = (String)$row[0]['ID'];
            $this->db->update('transfer_packet', $data, "ID = " . $id );    
        }
    }
}

PS:您的代码确实不可读,养成了使用正确的缩进的习惯-像NetBeans这样的许多IDE支持自动缩进,请查找它。

Codeigniter提供了用于创建XML的帮助程序和库。

像下面的示例一样,您可以使用Codeigniter库。

function write_xml() {
    // Load XML writer library
    $this->load->library('MY_Xml_writer');

    // Initiate class
    $xml = new MY_Xml_writer;
    $xml->setRootName('my_store');
    $xml->initiate();

    // Start branch 1
    $xml->startBranch('cars');

    // Set branch 1-1 and its nodes
    $xml->startBranch('car', array('country' => 'usa')); // start branch 1-1
    $xml->addNode('make', 'Ford');
    $xml->addNode('model', 'T-Ford', array(), true);
    $xml->endBranch();

    // Set branch 1-2 and its nodes
    $xml->startBranch('car', array('country' => 'Japan')); // start branch
    $xml->addNode('make', 'Toyota');
    $xml->addNode('model', 'Corolla', array(), true);
    $xml->endBranch();

    // End branch 1
    $xml->endBranch();

    // Start branch 2
    $xml->startBranch('bikes'); // start branch

    // Set branch 2-1  and its nodes
    $xml->startBranch('bike', array('country' => 'usa')); // start branch
    $xml->addNode('make', 'Harley-Davidson');
    $xml->addNode('model', 'Soft tail', array(), true);
    $xml->endBranch();

    // End branch 2
    $xml->endBranch();

    // Print the XML to screen
    $xml->getXml(true);
}

请阅读以下文档以创建XML。

文件https : //github.com/accentinteractive/xml_writer

暂无
暂无

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

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