We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

foreach get most recent date from xml

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

2
Contributors
4
Replies
3 Days
Discussion Span
3 Months Ago
Last Updated
11
Views
Question
Answered
adishardis
Junior Poster in Training
91 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,609 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

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

adishardis
Junior Poster in Training
91 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
foreach($xml->account as $mess){
 $date = '';
 $output = array();

to

$output = array();
foreach($xml->account as $mess){
 $date = '';
diafol
Keep Smiling
Moderator
10,609 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

Thanks!

adishardis
Junior Poster in Training
91 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by diafol

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0768 seconds using 2.77MB