zimmo Posted October 24, 2012 Share Posted October 24, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/269863-simple-xml-php/ Share on other sites More sharing options...
silkfire Posted October 24, 2012 Share Posted October 24, 2012 Make an empty array then add this to your code: $array[$entry->DrawnDate] = $file_name; This requires no date to be repeated (that is, each date must be unique). Quote Link to comment https://forums.phpfreaks.com/topic/269863-simple-xml-php/#findComment-1387506 Share on other sites More sharing options...
Barand Posted October 24, 2012 Share Posted October 24, 2012 As the names are unique it may be safer to have $array[$file_name] = $entry->DrawnDate and use asort() to preserve the keys Quote Link to comment https://forums.phpfreaks.com/topic/269863-simple-xml-php/#findComment-1387523 Share on other sites More sharing options...
salathe Posted October 24, 2012 Share Posted October 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269863-simple-xml-php/#findComment-1387542 Share on other sites More sharing options...
zimmo Posted October 25, 2012 Author Share Posted October 25, 2012 Thanks all for the help o this, I will be looking at using an array and sorting it from there. And also for the comments on my code, its all a learning curve. Thanks Again ) Quote Link to comment https://forums.phpfreaks.com/topic/269863-simple-xml-php/#findComment-1387652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.