Johan
Johan

Reputation: 211

Is there a way to sort by pubdate in descending order

i read my rss.xml file out in a php file. Now i want to sort the rss feed in descending pubdate order. Is this possible

<?php
$rss = simplexml_load_file('../../rssfeed.xml');
echo '<h1>'. $rss->channel->title . '</h1>';
foreach ($rss->channel->item as $item) {
   echo '<h2><a href="'. $item->link .'">' . $item->title . "</a></h2>";
   echo "<p>" . $item->pubDate . "</p>";
   echo "<p>" . $item->description . "</p>";
} 
?>

I was thinking about usort but i can't figure out how to get it to work...

xml rss code to test

<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
        <title>My RSS feed</title>

<item>
            <title>01Home</title>
<description>De hoofdpagina van flux cms. Op deze pagina worden de features en plugins van flux cms weergegeven. Flux cms is enorm aanpasbaar zodat je je website kan maken zoals jij dat wil. Zo kan je bvb een eigen teplate aanmaken zodat je website volledig aan jou verwachtingen voldoet.</description>
<link>http://localhost/index.php?p=01Home</link>
            <pubDate>2013 12 17</pubDate>
                </item><item>
            <title>02Gastenboek</title>
<description>Via het gastenboek kan je je bezoekers een bericht laten schrijven op je website dat leesbaar is voor alle andere sitegebruikers.</description>
<link>http://localhost/index.php?p=02Gastenboek</link>
            <pubDate>2013 12 05</pubDate>
                </item><item>
            <title>03Albums</title>
<description>Via de albums plugin heb je de mogelijkheid om 5 albums aan te maken en weer te geven. Via een makelijk upload en verwijder systeem kan je afbeeldingen toevoegen en weer verwijderen. De albums zijn aanklikbaar en worden in een ooie slider weergegeven via een pop up.</description>
<link>http://localhost/index.php?p=03Albums</link>
            <pubDate>2013 12 18</pubDate>
                </item><item>
            <title>04Videos</title>
<description>videos.</description>
<link>http://localhost/index.php?p=04Videos</link>
            <pubDate>2013 12 18</pubDate>
                </item><item>
            <title>05Poll</title>
<description>Via deze plugin kan je makkelijk een poll toevoegen ergens op je website. Je kan een hoofdvraag stellen en max 4 mogelijke aantwoorden aanklikken. Handig om bvb te weten wat gebruikers van je site vinden.</description>
<link>http://localhost/index.php?p=05Poll</link>
            <pubDate>2013 12 05</pubDate>
                </item><item>
            <title>06Events</title>
<description>Events van de kalender</description>
<link>http://localhost/index.php?p=06Events</link>
            <pubDate>2013 12 17</pubDate>
                </item><item>
            <title>07Nieuwsbrief</title>
<description>Nieuwsbrief.</description>
<link>http://localhost/index.php?p=07Nieuwsbrief</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>08Facebook</title>
<description>facebook</description>
<link>http://localhost/index.php?p=08Facebook</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>09Zoeken</title>
<description>zoeken.</description>
<link>http://localhost/index.php?p=09Zoeken</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>10statistieken</title>
<description>statistieken.</description>
<link>http://localhost/index.php?p=10statistieken</link>
            <pubDate>2013 12 17</pubDate>
                </item><item>
            <title>11Profielen</title>
<description>profielen.</description>
<link>http://localhost/index.php?p=11Profielen</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>12Privebericht</title>
<description>Plugin waarbij de sitegebruiker een bericht kan versturen naar de webbeheerder.</description>
<link>http://localhost/index.php?p=12Privebericht</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>13Blog</title>
<description>blog.</description>
<link>http://localhost/index.php?p=13Blog</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>14Kalender</title>
<description>kalender.</description>
<link>http://localhost/index.php?p=14Kalender</link>
            <pubDate>2013 12 10</pubDate>
                </item><item>
            <title>15Search</title>
<description>Met deze functie kan je intern alle paginas zoeken. De zoekfunctie zoekt op de paginabeschrijvingswoorden die je opgeeft bij het creeren en bewerken van de pagina's.</description>
<link>http://localhost/index.php?p=15Search</link>
            <pubDate>2013 12 04</pubDate>
                </item><item>
            <title>Album five</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album five</link>
            <pubDate>2013 07 19</pubDate>
                </item><item>
            <title>Album four</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album four</link>
            <pubDate>2013 03 23</pubDate>
                </item><item>
            <title>Album one</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album one</link>
            <pubDate>2013 12 11</pubDate>
                </item><item>
            <title>Album three</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album three</link>
            <pubDate>2013 03 23</pubDate>
                </item><item>
            <title>Album two</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album two</link>
            <pubDate>2013 03 23</pubDate>
                </item><item>
            <title>antiquar</title>
<description>TEST</description>
<link>http://localhost/index.php?p=antiquar</link>
            <pubDate>2013 06 13</pubDate>
                </item><item>
            <title>login</title>
<description>TEST</description>
<link>http://localhost/index.php?p=login</link>
            <pubDate>2013 06 11</pubDate>
                </item><item>
            <title>news</title>
<description>TEST</description>
<link>http://localhost/index.php?p=news</link>
            <pubDate>2013 04 01</pubDate>
                </item><item>
            <title>registreer</title>
<description>TEST</description>
<link>http://localhost/index.php?p=registreer</link>
            <pubDate>2013 04 23</pubDate>
                </item></channel></rss>

Upvotes: 1

Views: 1261

Answers (2)

Dwza
Dwza

Reputation: 6565

i would do it like this...

<?php
$rss = simplexml_load_file('../../rssfeed.xml');

echo '<h1>'. $rss->channel->title . '</h1>';

foreach ($rss->channel->item as $item) {
    $tmp = str_replace(" ","",$item->pubDate);
    $div[$tmp][] = "<h2><a href='{$item->link}'>{$item->title}</a></h2>" .
                   "<p>{$item->pubDate}</p>" .
                   "<p>{$item->description}</p>";
}
array_multisort($div,SORT_DESC);
foreach($div as $d)
{
    foreach($d as $output) echo $output;
}

?>

by the way... i would always collect data in a array or variable befor echo'ing it. if you have some issues you can easy turn of the output on one position and don't need to edit your code in 50 lines :) i added replace to get the whitespaces out of your pupdate. i don't know if whitespaces causes errors when you use them in arraykey. anyway... this is more clean :) and it's a 2 demensional array because you have equal pupdates.

Upvotes: 3

Patrick Maciel
Patrick Maciel

Reputation: 4944

Just do that:

usort($rss->channel->item, function($a,$b) {
    return strtotime($a->pubdate)-strtotime($b->pubdate);
});

Now, complete code:

<?php
  $rss = simplexml_load_file('../../rssfeed.xml');
  $json_rss = json_encode($xml);
  $rss = json_decode($json_rss,TRUE);

  usort($rss->channel->item, function($a,$b) {
      return strtotime($a->pubdate)-strtotime($b->pubdate);
  });

  echo '<h1>'. $rss->channel->title . '</h1>';

  foreach ($rss['channel']['item'] as $item) {
     echo '<h2><a href="'. $item['link'] .'">' . $item['title'] . "</a></h2>";
     echo "<p>" . $item['pubDate'] . "</p>";
     echo "<p>" . $item['description'] . "</p>";
  } 
?>

Upvotes: 1

Related Questions