开发者

Trying to sort the posts from feeds based on date instead of showing the results per blog

Below is a part of a full script I builted that gets description, images, title from a blog's rss. What I do is use this code for each RSS I want to include in my page, having the results showing like

1st Blog (showing 25 results) and below this

2st Blog (showing another 25 results)

and so on.

My goal is to sort the results based on date and not per blog. It is usefull to say that all of these posts have timestamps like <pubDate>Wed, 08 Jun 2011 14:12:00 +0000</pubDate>.

Here is my code that shows correctly the Title of the post.

<?php
$myrss = new DOMDocument;
libxml_use_internal_errors(TRUE);
$myrss->load ('LINK TO RSS'' . $i; } ?>

This is the structure of a RSS

<item><guid isPermaLink=开发者_StackOverflow"false"></guid><pubDate></pubDate><atom:updated></atom:updated><title></title><description></description><link></link><author></author></item>


Roughly, here is how I would do it.

$arr_rss_feeds = array( 'link1', 'link2', 'link3', ... );
$arr_posts = array();

foreach( $arr_rss_feeds as $rss_feed )
{
    //## Connect to RSS feed, get posts (not shown)
    $arr_posts[ strtotime( 'post date field here' ) ] = array( 
                'title'   => 'Title here'
            ,   'url'     => 'URL here'
    );
}

//## Sort the posts by timestamp (reverse, so newest first)
krsort( $arr_posts );

foreach( $arr_posts as $post )
{
    //## Output your list using the 'title' and 'url' from above.
}

Basically, you are putting all of the posts from all of the feeds into one array with a key of their Unix timestamp, sorting the array based on that timestamp, and then outputting them in order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜