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

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支持自动缩进,请查找它。

07-28 06:01