1,105,208 Community Members

foreach get most recent date from xml

Member Avatar
adishardis
Junior Poster
101 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I use this script to extract and insert the desired variables to mysql.It does the job.

$xml = new SimpleXMLElement('file.xml');
foreach($xml->loanaccount as $mess){
    $account_number= mysql_real_escape_string($mess->account_number);
    $main= mysql_real_escape_string($mess->main);
    $type= mysql_real_escape_string($mess->{'application-array'}->{'application-type'}),
    $date= mysql_real_escape_string($mess->{'application-array'}->{'application-date'}),
//insert into databse                     
    mysql_query("INSERT INTO account (account_number, main) VALUES ('$account_number', '$main')")
or die(mysql_error());
    echo "inserted into mysql<br /><br />";
//show updated records            
    printf ("Records inserted: %d\n", mysql_affected_rows());  
}

The problem I have is that the xml can contain several application-array and I need to insert the application-date and application-type from the most recent application-array.

This is what the xml looks like.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account-information report-date="2012-12-31T23:59:59.000+01:00" xmlns="http://www.XXX.SE/XXX">
    <account>
        <account-number>00000000</account-number>
        <status>XXX</status>
        <application-array>
            <application-type>NEW</application-type>
            <application-date>XXX</application-date>
        </application-array>
        <application-array>
            <application-type>TOP</application-type>
            <application-date>XXX</application-date>
        </application-array>
        <account-information-type>
            <first-name-main></first-name-main>
        </account-information-type>
    </account>
</account-information>

So how do I change my foreach statement to capture the most recent application-type and date from the application array.

Thanks
Adamski

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 
$xml = simplexml_load_file('me.xml');
$x = 0;
foreach($xml->account as $mess){
    $date = '';
    foreach($mess->{'application-array'} as $aa){
        if((string) $aa->{'application-date'} > $date){
            $output[$x] = array($aa->{'application-date'}, $aa->{'application-type'});  
            $date = $aa->{'application-date'};  
        }
    }
    $x++;   
}
echo "<pre>";
print_r($output);
echo "</pre>";

With this sort of XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account-information report-date="2012-12-31T23:59:59.000+01:00" xmlns="http://www.XXX.SE/XXX">
    <account>
        <account-number>00000000</account-number>
        <status>BORN</status>
        <application-array>
            <application-type>NEW</application-type>
            <application-date>2013-09-18</application-date>
        </application-array>
        <application-array>
            <application-type>TOP</application-type>
            <application-date>2014-09-18</application-date>
        </application-array>
        <account-information-type>
            <first-name-main></first-name-main>
        </account-information-type>
    </account>
     <account>
        <account-number>00000001</account-number>
        <status>DEAD</status>
        <application-array>
            <application-type>NEWEST</application-type>
            <application-date>2005-09-18</application-date>
        </application-array>
        <application-array>
            <application-type>TIPS</application-type>
            <application-date>2010-09-18</application-date>
        </application-array>
        <account-information-type>
            <first-name-main></first-name-main>
        </account-information-type>
    </account>
</account-information>

Gives:

Array
(
    [0] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 2014-09-18
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => TOP
                )

        )

    [1] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 2010-09-18
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => TIPS
                )

        )

)

Which is what I was expecting. Not sure if that's what you want though.

Member Avatar
adishardis
Junior Poster
101 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That's what I'm looking for! Now I tried to accomadate it to insert it into mysql db but nothing happends, no errors or nothing. And if I look att the printed output it only shows one (last) account from the xml but even that line doesn't get inserted into my db.

Maybe I'm going about it the wrong way but here's what I tried:

$x = 0;
foreach($xml->account as $mess){
 $date = '';
 $output = array();
  $account_number= $mess->{'account-number'};
  $firstname= $mess->{'information-type'}->{'first-name-main'};
  $lastname= $mess->{'information-type'}->{'last-name-main'};
  $email= $mess->{'information-type'}->{'email-main'};
        foreach($mess->{'application-array'} as $aa){
            if((string) $aa->{'application-date'} > $date){
                $output[$x] = '('.$account_number.',"'.mysql_real_escape_string($firstname).'","'.mysql_real_escape_string($lastname).'","'.mysql_real_escape_string($email).'", "'.mysql_real_escape_string($aa->{'application-type'}).'",'. $aa->{'application-date'}.')';  
                $date = $aa->{'application-date'};  
        }
    }
    $x++;
}  
$sql = 'INSERT INTO ignore delay account (`account_number`, `first_name`, `last_name`, `email`, `application-type`, `application-date`) VALUES '.implode(',', $output)or die(mysql_error());

mysql_query($sql);  

echo "<pre>";
print_r($sql);
echo "</pre>";/* 

Thanks
/Adam

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
1
 
foreach($xml->account as $mess){
 $date = '';
 $output = array();

to

$output = array();
foreach($xml->account as $mess){
 $date = '';
Member Avatar
adishardis
Junior Poster
101 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks!

Question Answered as of 1 Year Ago by diafol
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: