簡體   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