Jump to content

Simple Xml Php


zimmo

Recommended Posts

Can anyone help me with this xml as I very new to xml

 

I have a folder on a server that gets xml files delivered to every day, the files have random unique names such as 2ee05c45-e004-4f52-955f-ba6f758d1bd3.xml so they are randomly listed in the folder by name. Now the script below reads through this directory and loads the results. Great, but what I need to do now is to order these results so that they are in date order. Now the date is contained within a field on each of the files. Is this possible and what do I need to do to sort them in date order, again very limited knowledge of xml, so any help most grateful..

 

<?php
echo "<h3>Draw Name</h3>";
echo "<h3>Draw Date</h3>";
echo "<h3>Letters/Numbers</h3>";
echo "<h3>No of Shares</h3>";
{
    foreach (new DirectoryIterator('/location/to/folder/with/xml/files/in/') as $oFile)
    {
        if( $oFile->isFile() && ! $oFile->isDot() )
        {

           $oXML = simplexml_load_file( $oFile->getPathname() );
           // loop through simplexml object and assign value to variables

foreach ($oXML->DrawResult as $entry){
$DrawName = $entry->DrawName;
$DrawnDate = $entry->DrawnDate;
$DrawnSymbols = $entry->DrawnSymbols;
$NoOfShares = $entry->NoOfShares;

// output the value of 'name' attribute of the first <title>
echo "<h4>$DrawName</h4>";
echo "<h4>$DrawnDate</h4>";
echo "<h4>$DrawnSymbols</h4>";
echo "<h4>$NoOfShares</h4>";
}

   }

     }
}
?>

 

Here is a snippet from the xml file with the date in:

<DrawnDate>10/23/2012 8:04:53 PM</DrawnDate>

 

 

So just need to find a way to sort all these so we have the most recent first. Thanks again ;o)

Link to comment
Share on other sites

Great, but what I need to do now is to order these results so that they are in date order. Now the date is contained within a field on each of the files. Is this possible and what do I need to do to sort them in date order

 

As the others said, one of the easier ways would be to create an array containing enough information to sort the files as you need. This array could contain all of the information that you are going to display, or it could just be a stepping stone to getting the list of files in the right order: that's up to you.

 

I would go with Barand's suggestion of creating an array mapping file names to DrawnDates.

 


 

While you're here, there are a few minor points about your code that I hope you wouldn't mind me mentioning.

 

  • The FilesystemIterator is preferred over the older, slightly quirky, DirectoryIterator. In your case, only the class name needs to be changed to use it.
  • By using isFile(), the second condition will never be met: when $oFile is a file, then it can not be a dot-directory. Your code might as well be if ($oFile->isFile()) {
  • Your XML files only have one DrawResult (judging by your example on Dev Shed), so there is no need to loop over (all one of) them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.