1

I am pulling a list of blog pages from a .XML file and printing the 2 newest entries for a web page. I however have no idea how to sort the .XML files by pubDate or file_edited.

The code successfully retrieves the files and prints the two newest entries.

Here is the PHP code block that retrieves the files and prints them.

<?php
date_default_timezone_set('Europe/Helsinki');

/* XML Source URL:s */
$pages=("blog/data/other/pages.xml");

/* XML Doc Conversions */
$xmlDoc = new DOMDocument();

echo "<div class='blog_article_wrapper'>";

function myFunction($x){
  // Run 2 times, skip first file and stop loop.
  for ($i=1; $i<=2; $i++) {

  //Get "Title
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;

  //Get "Date" from .XML document.
  $item_date=$x->item($i)->getElementsByTagName('pubDate')
  ->item(0)->childNodes->item(0)->nodeValue;

  //Get "URL" from .XML document.
  $item_url=$x->item($i)->getElementsByTagName('url')
  ->item(0)->childNodes->item(0)->nodeValue;

  //Get "Author" from .XML document.
  $item_author=$x->item($i)->getElementsByTagName('author')
  ->item(0)->childNodes->item(0)->nodeValue;  

  //Format date and author
  $item_date = date('d.m.Y', strtotime($item_date));
  $item_author = ucfirst(strtolower($item_author));

  //Get content data from specifix .XML document being iterated in loop
  $url=("blog/data/pages/" . $item_url . ".xml");
  $xmlDoc = new DOMDocument();
  $xmlDoc->load($url);
  $y=$xmlDoc->getElementsByTagName('content')->item(0)->nodeValue;    

  //Limit content to 150 letters and first paragraph tag.
  $start = strpos($y, '<p>="') + 9;
  $length = strpos($y, '"</p>') - $start;
  $src = substr($y, $start, $length);
  $item_content = "\"" . (substr($src, 0, 150)) . "...\"";

  // Page specific code for output comes here.
  }
}

//Call loop and iterate data
$xmlDoc->load($pages);
$x=$xmlDoc->getElementsByTagName('item');
myFunction($x);
?>

Any advice, code or articles pointing in the right direction would be much appreciated.

Thank you!

Halycon
  • 78
  • 8
  • your problem could be generalized to "how to find top 2 entries in a collection", thus it could seen as an algorithm problem. I am sure you know how to find the newest entry (basically, the entry with the "maximum" date), finding the newest two entries can be achieved pretty much in the same way except that while you iterate through your entries you compare the current one with the top two ones you found until that step and update appropriately. no need to sort anything – cobarzan Jan 01 '16 at 22:25
  • I could write another loop to sort through the results and sort them according to the "pubDate" specified in the .XML document but this would not by far be the best way because there will be a number of documents to be called. I would think that the best way would be to sort the files by file_edited, and then iterate the newest entries through the loop..? – Halycon Jan 02 '16 at 02:57

1 Answers1

0

I figured this out my self using another stackoverflow question and php.net

//Directory where files are stored.
$folder = "blog/data/pages/"; 

$array = array();

//scandir and populate array with filename as key and filemtime as value.
foreach (scandir($folder) as $node) {
   $nodePath = $folder . DIRECTORY_SEPARATOR . $node;

   if (is_dir($nodePath)) continue;
   $array[$nodePath] = filemtime($nodePath);
 }

//Sort entry and store two newest files into $newest
arsort($array);
$newest = array_slice($array, 0, 2);

// $newest is now populated with name of .XML document as key and filemtime as value
// Use built in functions array_keys() and array_values() to access data 

?>

I can now modify the original code in the question to use only these two outputted files for retrieving the desired data.

Community
  • 1
  • 1
Halycon
  • 78
  • 8