You are not logged in.

Announcement

Support Has Moved!

SimplePie support has moved to Yahoo! Groups, and bug tracking has moved to bugs.simplepie.org. This support site will remain available as a read-only archive for the foreseeable future.

#1 27 August 2006 14:06:10

Olargues
New member
Registered: 27 August 2006
Posts: 1

Sort only by date

First of all, thank you very much. SimplePie is easy to use and install, works great.

However, I can't find my way around.

I am using the Multiplefeeds add-on.

Say that I would like to get the latest posts from 3 different feeds, I have right now the following code:

foreach ($feed->get_items(0, 5) as $item) {

It display the 5 latest posts from the 3 feeds.

But I would like to get instead the 15 latest posts, it could be 8 from feed #1, 4 from feed #2, 3 from feed #3.

What would be the code to use instead ?

I hope I made myself clear.

Thank you very much for your help
Phil

Last edited by Olargues (27 August 2006 19:12:12)

Offline

 

#2 27 August 2006 22:14:17

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Sort only by date

I haven't tested this code yet, but it's a slight modification to the code from index.php from the multifeeds demo.

Code:

<?php
/*****************************************************************
PREPARATION AND PROCESSING
*****************************************************************/

// Be sure to include SimplePie.
require_once('simplepie.inc');

// Create my list of feeds to use
// I've checked that all of these feeds have per-item dates
$myfeeds = array();
$myfeeds[] = 'http://rss.news.yahoo.com/rss/topstories';
$myfeeds[] = 'http://news.google.com/?output=rss';
$myfeeds[] = 'http://digg.com/rss/containertechnology.xml';

$numberofitems = array();
$numberofitems[] = 8;
$numberofitems[] = 3;
$numberofitems[] = 4;

// This will be used to store and sort the feeds by date
$multifeeds = array();

// Keep track of which feed we're on
$count = 0;

// Go through each feed in the above array, parse it, and add specific chunks of data to the $multifeed array
foreach($myfeeds as $url) {

    // Set your own configuration options as you see fit.
    $feed = new SimplePie();
    $feed->feed_url($url);
    $feed->cache_max_minutes(30);
    $feed->replace_headers(true);
    $feed->init();

    // We're going to loop through the items in the feed; starting at the beginning, and returning a max of 10 items.
    for ($x=0; $feed->get_item_quantity($numberofitems[$count]); $x++) {
        $item = $feed->get_item($x);

        /*
         * We're going to take data from the $item and put it together into a single string, delimiting each chunk 
         * with five colons (you can delimit however you want, but it's unlikely that you'll come across five 
         * consecutive colons in the content of a feed).
         */

        // Start with a blank slate
        $data = '';

        // We're going to start with milliseconds since Unix epoch (this is the datestamp that we'll sort by)
        $data .= $item->get_date('U') . ':::::';

        // We'll choose a single timezone (GMT) to base all dates/times on.
        $data .= gmdate('j M Y | g:ia \G\M\T', $item->get_date('U')) . ':::::';

        // Get the title of the posting
        $data .= $item->get_title() . ':::::';

        // Get the permalink for the posting
        $data .= $item->get_permalink() . ':::::';

        // Get the description content for the posting
        $data .= $item->get_description() . ':::::';

        // Besides $item data, we'll also get the title of the $feed we're pulling this from
        $data .= $feed->get_feed_title() . ':::::';

        // Lastly, we'll get the permalink to the $feed that we're pulling this from.
        // Since it's the last one, we don't need to add the delimiter to the end.
        $data .= $feed->get_feed_link();

        // Place this whole thing into the next available spot in the $multifeeds array.
        $multifeeds[] = $data;
    }

    // We're done with $feed for this round of the loop, so we'll wipe it out so we can loop back and start fresh.
    unset($feed);
    
    // Increment the feed counter
    count++;
}

// When we're done looping through the feeds and collecting our data, we'll reverse sort all of the feeds by seconds since Unix epoch (newest to oldest)
rsort($multifeeds);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Multiple feeds test</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>

<body>

<div id="site">
    <h4>Recently Posted&hellip;</h4>
    <ol>

    <?php
    // Go through each and every feed in $multifeeds
    foreach ($multifeeds as $posting) {

        // Break it all back into chunks
        $data = explode(':::::', $posting);
    ?>
        <li>
            <strong><a href="<?php echo $data[3]; ?>"><?php echo $data[2]; ?></a></strong><br />
            <span style="font-size:0.8em; color:#999;">&rarr; <a href="<?php echo $data[6]; ?>"><?php echo $data[5]; ?></a> | <?php echo $data[1]; ?></span><br />
            <?php echo $data[4]; ?>
        </li>
    <?php } ?>

    </ol>
</div>

</body>
</html>

Try it and let me know if it works or not.


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#3 28 August 2006 21:11:04

mattw
New member
Registered: 18 August 2006
Posts: 2

Re: Sort only by date

I want to combine two feeds, and I want to display the 10 most recent items, regardless of where they come from.

If Source A has one post in two weeks and Source B has 15 posts in the last day, I want the output to be zero posts from Source A and 10 posts from Source B. If the posting schedule is exactly equal, I want five from each.

It won't matter to me how many items from each source are posted, I just want to have the 10 most recent. (I believe that's what the original question was asking, too -- not just a way to hardcode the number of items from each source). Will the Multifeeds add-on do this?

Offline

 

#4 29 August 2006 10:53:01

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Sort only by date

mattw -- the multifeeds demo is just some custom-written code that utilizes SimplePie to pull the latest items from multiple feed sources and display them.

What you can do is tell it to return the last 10 posts -- period -- and it will (of course, the feed posts must have a date associated with them in order to be able to sort properly).

In order to do what you're asking, get the latest multifeeds package (I tweaked it a bit a couple of days ago).  Look for line 93 and change this:

Code:

foreach ($multifeeds as $posting) {

... to this...

Code:

for ($x=0; $x<10; $x++) {
    $posting = $multifeeds[$x];

Doing do will limit the output to 10 items.  They will be whatever the most recent 10 are out of all of the feeds pulled.

Hope this helps!


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#5 29 August 2006 22:18:52

mattw
New member
Registered: 18 August 2006
Posts: 2

Re: Sort only by date

Skyzyx,

Thanks for the help -- that's exactly what I was looking for!

Not to press my luck, but I have a follow-up question to do something a bit more complex -- what's the most efficient way to display multiple blocks of multiple feeds?

That's probably a little confusing -- check out this URL to see what I'm talking about:
http://www.motorcityblogs.com
user: test1
pass: test1

Here's what I currently have above my header info:

Code:

$myAfeeds = array(
        "tigers"=> "http://www.exampleA.com/feed/",
        "lions"=>"http://exampleB.com/feed/",
        "pistons"=> "http://exampleC.com/feed/"
        );

And later in my html I have this:

Code:

<div class="feedarea">
<br/>
    <div class="title">
        <h1><img src="images/title_A.gif"/></h1><br/>
    </div>
    <div class="title">
        <h1><img src="images/title_B.gif"/></h1><br/>
    </div>
    <div class="title">
        <h1><img src="images/title_C.gif"/></h1><br/>
     </div>
</div>

<div class="feedarea">

<?php
foreach ($myAfeeds as $team=>$url) { 
    $feed = new SimplePie($url, $cache);

    if ($feed->data) {
        echo '<div class="feed"> <span class="rsslink"> ';

        $max = $feed->get_item_quantity(10);
        for ($x = 0; $x < $max; $x++) {
            $item = $feed->get_item($x);
            echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a>'
            ;
        }

        echo '</span></div>';
    }
    unset($feed);
}
?>

</div>

What I'd like to do is add multiple feeds to each team. Using the solution you provided above, I'm guessing I could do this with a nested array inside $myAfeeds, right?

Is this too complex for you to quickly show me how it'd be done? If so, could you point me in the right direction of a general PHP tutorial that might be applicable? As you might have guessed, I'm only a couple of weeks into teaching myself PHP -- I really appreciate all of the help I've received by reading this forum and your other tutorials.

Offline

 

#6 23 April 2007 15:30:25

andy7629
Member
Registered: 22 March 2007
Posts: 10

Re: Sort only by date

Is the above possible now? If so how?

Offline

 

#7 23 April 2007 22:54:06

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Sort only by date

Is what possible now?


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#8 24 April 2007 06:23:52

andy7629
Member
Registered: 22 March 2007
Posts: 10

Re: Sort only by date

"I want to combine two feeds, and I want to display the 10 most recent items, regardless of where they come from.

If Source A has one post in two weeks and Source B has 15 posts in the last day, I want the output to be zero posts from Source A and 10 posts from Source B. If the posting schedule is exactly equal, I want five from each.

It won't matter to me how many items from each source are posted, I just want to have the 10 most recent. (I believe that's what the original question was asking, too -- not just a way to hardcode the number of items from each source). Will the Multifeeds add-on do this?"

Offline

 

#9 2 May 2007 23:42:06

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Sort only by date

Yes, this is exactly what the multifeeds code is intended to do.


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#10 14 December 2007 17:48:21

modifiedcontent
Member
Registered: 24 December 2006
Posts: 20

Re: Sort only by date

I'd like to try Skyzyx' solution from 29 August 2006, but don't see this line:

Code:

foreach ($multifeeds as $posting) {

The nearest thing in my code is this:

Code:

foreach($feed->get_items(0,1) as $item):

I use SimplePie version "Razzleberry" (1.0.1). Not sure if the 2006 solution still applies.

I've tried to use the suggested code by replacing some words, but couldn't get it to work. I don't know PHP. The problem may just be a { or : or ;

Offline

 

#11 31 December 2007 16:59:34

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Sort only by date

First of all, what are you trying to accomplish?

Secondly, have you seen the 5th minimum requirement for using SimplePie? wink


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson