Breadcrumbs

Coding an All-in-one RSS Feed

One of the cool new things that we have added at Lowter recently is an all-in-one RSS feed (external link) that contains both our articles and our blog entries. Granted, the feed doesn't contain "all" of Lowter's content, such as new forum threads and blog comments, but it is very encompassing nonetheless. Coding such an all-in-one RSS feed is pretty easy!

First, I'm going to assume that you want to make a combined feed of both your articles and blog entries. You can easily change the code to fit whatever combining scenario you have, such as different sources of content or more than two sources. Retrieve your blog entries and articles (probably via database) and make sure they are formatted into arrays like below:

Code: PHP

<?php
$latest_articles 
= array(
    [
0] => array(
        
'type' => 'article',
        
'title' => '',
        
'date' => '',
        
'url' => '',
        
'summary' => '',
    )
);

$latest_blogs = array(
    [
0] => array(
        
'type' => 'blog',
        
'title' => '',
        
'date' => '',
        
'url' => '',
        
'summary' => '',
    )
);
?>

Make sure that you add the type key to each content item's array. This data is essential when formatting the different URLs in the actual RSS feed. You may need to re-loop through the data if you retrieve it from a database:

Code: PHP

<?php
foreach ($latest_articles as &$article)
{
    
$article['type'] = 'article';
}

foreach (
$latest_blogs as &$entry)
{
    
$entry['type'] = 'blog';
}
?>

Also, if you want x number of the most recent items in your feed, you need to make sure that you retrieve x number of both articles and blog entries. For example, if you want to show the latest 25 items, then query your database for the latest 25 items of each source because this accounts for the case if all the latest items are of one particular source.

Now you need to merge the two arrays (array_merge() (external link)) and then sort the newly merged array by the date:

Code: PHP

<?php
function multi_array_sort($sort_by$array$order_by SORT_ASC)
{
    
$values = array();
    
    foreach (
$array as $key => $row)
    {
        
$values[$key]  = strtolower($row[$sort_by]);
    }
    
    
array_multisort($values$order_by$array);
    return 
$array;
}

$combined_content array_merge($latest_articles$latest_blogs);
$combined_content multi_array_sort('date'$combined_contentSORT_DESC);
?>

I am using the multi_array_sort() function I include in all my applications to sort database results in a multi-dimensional array. I'm not going to go into detail of the code for this function, but just know this sorts everything by the descending date. Now you need to cut the array down into just the latest 25 items using array_splice() (external link):

Code: PHP

<?php
array_splice
($combined_content25);
?>

Now that you have an array with the combined latest 25 content items, you just need to format it into an RSS feed. Our final code looks something like this, depending on how you retrieved your content:

Code: PHP

<?php

/**
 * Sort a multi-dimensional array
 * 
 * @param string $sort_by Which field to sort by
 * @param array $array Array to sort
 * @param string $order_by How to sort the array
 * @return array
 */
function multi_array_sort($sort_by$array$order_by SORT_ASC)
{
    
$values = array();
    
    foreach (
$array as $key => $row)
    {
        
$values[$key]  = strtolower($row[$sort_by]);
    }
    
    
array_multisort($values$order_by$array);
    return 
$array;
}

$latest_articles $this->articles->find_all('id DESC'25);
$latest_blogs $this->blogs->find_all('id DESC'25);

$combined_content array_merge($latest_articles$latest_blogs);
$combined_content multi_array_sort('date'$combined_contentSORT_DESC);
array_splice($combined_content25);

// Change MIME type
header("content-type: application/rss+xml");
?>
<?php 
echo '<?xml version="1.0" encoding="utf-8"?>' ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Lowter - New Content</title>
        <description>Lowter.com is an electronic magazine covering web development, in the form of articles, blogs, forums, newsletters, and podcasts. Lowter.com primarily covers PHP, JavaScript, (X)HTML, CSS, accessibility, usability, application design, and programming theory.</description>
        <language>en-us</language>
        <link>http://www.lowter.com</link>
        <copyright>Copyright 2004-2008 Lowter</copyright>
        <category>web development</category>
        <atom:link href="http://www.lowter.com/syndication/all" rel="self" type="application/rss+xml" />
        
        <?php foreach ($combined_content as $content) : ?>
            <item>
                <title><?php echo $content['title'?></title>
                <description><![CDATA[<?php echo $content['summary'?>]]></description>
                <link>http://www.lowter.com/<?php echo ($content['type'] == 'article') ? 'article' 'blogs' ?>/<?php echo $content['url'?></link>
                <guid isPermaLink="true">http://www.lowter.com/<?php echo ($content['type'] == 'article') ? 'article' 'blogs' ?>/<?php echo $content['url'?></guid>
                <pubDate><?php echo date('r'$content['date']) ?></pubDate>
                <dc:creator><?php echo $content['author'?></dc:creator>
            </item>
        <?php endforeach; ?>
    </channel>
</rss>

Tags: