<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>CSSKarma &#187; simplepie</title> <atom:link href="http://www.csskarma.com/blog/tag/simplepie/feed/" rel="self" type="application/rss+xml" /><link>http://www.csskarma.com/blog</link> <description>display your style</description> <lastBuildDate>Tue, 31 Jan 2012 15:18:44 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Selectable RSS with SimplePie and jQuery</title><link>http://www.csskarma.com/blog/selectable-rss/</link> <comments>http://www.csskarma.com/blog/selectable-rss/#comments</comments> <pubDate>Mon, 17 Aug 2009 05:45:45 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Development]]></category> <category><![CDATA[advanced]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[rss]]></category> <category><![CDATA[simplepie]]></category> <category><![CDATA[ui]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=632</guid> <description><![CDATA[I was building an RSS reader for a project last week and it turned out to be a cool little app. So I thought I&#8217;d share some of the code. View demo Download demo files Let&#8217;s start off with the HTML we need to get some user interaction with the form. It&#8217;s a pretty basic, [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://www.csskarma.com/images/articles/simplepieloader.jpg" alt="banner" /></p><p>I was building an RSS reader for a project last week and it turned out to be a cool little app. So I thought I&#8217;d share some of the code.</p><p
class="demo"><a
href="http://www.csskarma.com/articles/examples/simplepieloader/">View demo</a></p><p
class="demo"><a
href="http://www.csskarma.com/articles/examples/simplepieloader/simplepieloader.zip">Download demo files</a></p><p>Let&#8217;s start off with the HTML we need to get some user interaction with the form. It&#8217;s a pretty basic, just some of checkboxes.</p><p><img
src="http://www.csskarma.com/images/articles/simplepie_form.png" alt="form with 4 checkboxes" /></p><h5>HTML</h5><pre><code>&lt;form action="" method="post" id="selection"&gt;
  &lt;h2&gt;Choose your news sources&lt;/h2&gt;
 <span>&lt;!--
  Setting all the checkbox names to "source[] helps us build the array on submit"
  We're also adding the RSS feed url to the checkbox value so we can save it in the array values
  --&gt;</span>
  &lt;ul&gt;
  &lt;li&gt;&lt;input type="checkbox" id="csskarma" name="source[]" value="http://www.csskarma.com/blog/feed/"/&gt;&lt;label for="csskarma"&gt;CSSKarma&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" id="cnn" name="source[]" value="http://rss.cnn.com/rss/cnn_topstories.rss"/&gt;&lt;label for="cnn"&gt;CNN&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" id="espn" name="source[]" value="http://sports-ak.espn.go.com/espn/rss/news"/&gt;&lt;label for="espn"&gt;ESPN&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" id="vitamin" name="source[]" value="http://feeds.feedburner.com/vitaminmasterfeed"/&gt;&lt;label for="vitamin"&gt;Think Vitamin&lt;/label&gt;&lt;/li&gt;
  &lt;/ul&gt;

  <span>&lt;!-- Setting the submit name to "btn" --&gt;</span>
  &lt;input type="submit" value="submit" name="btn" id="btn-submit"/&gt;
  &lt;/form&gt;
  &lt;/div&gt;&lt;!--/#selectnews--&gt;</code></pre><p>The only real thing worth noting in the form code are the names of the checkboxes. We use all the same name to group them together and the [ ] helps create the array on submit. I just chose to use <code>source[]</code> because it made sense to me, but you can use anything as long as the names are consistent and the brackets are there. Just adjust your PHP accordingly.</p><h5>SimplePie PHP</h5><pre><code>&lt;?php
<span>/* Get the SimplePie library */</span>
require_once ('inc/simplepie.inc');

<span>/* Create a new instance of SimplePie */</span>
$feed = new SimplePie();

<span>/* Get array */</span>
<span>// If the form has been submitted and the user selected news sources</span>
if (isset ($_POST['btn']) &amp;&amp; isset($_POST['source'])) {

    <span>// Save the array of news sources to a variable $checked</span>
    $checked = $_POST['source'];

    <span>// Pass the array of news sources through the SimplePie set_feed_url() function</span>
    $feed-&gt;set_feed_url($checked);

    <span>// Also, save this data to a cookie called "feedurls" that will expire when the browser closes</span>
    setcookie('feedurls', serialize($checked));

<span>// If the form wasn't submitted look for the "feedurls" cookie</span>
} else if (isset ($_COOKIE['feedurls'])) {

	<span>// If the cookie was found, pass the data through set_feed_urls()</span>
	$feed-&gt;set_feed_url(unserialize($_COOKIE['feedurls']));
}

<span>/* Set item limit */</span>
$feed-&gt;set_item_limit(3);

<span>/* Enable caching */</span>
$feed-&gt;enable_cache(true);

<span>/* Provide the caching folder */</span>
$feed-&gt;set_cache_location('cache');

<span>/* Set the amount of seconds you want to cache the feed */</span>
$feed-&gt;set_cache_duration(1800);

<span>/* I don't know what "init" means */</span>
$feed-&gt;init();

<span>/* Handle the content type (atom, RSS...) */</span>
$feed-&gt;handle_content_type();
?&gt;</code></pre><p>This is nothing that can&#8217;t be read in the comments, but the guts of the PHP comes in right under the &#8220;Get Array&#8221; comment. We&#8217;re checking to see if the form was submitted and the user selected news sources; if so, dump the values into SimplePie and save them to a cookie we can refer to later. The initial idea was for this to live at a pretty open place (a computer lab or something), that&#8217;s why the cookie expires on browser close.</p><p>I limited the per-feed output to 3 to keep it a little clean, but you can set that to whatever you want. It&#8217;d be pretty easy to let the user choose this as well with a select menu:</p><pre><code>&lt;select name="displayNumber"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="2"&gt;2&lt;/option&gt;
&lt;option value="3"&gt;3&lt;/option&gt;
&lt;/select&gt;</code></pre><p>And replace <code>$feed-&gt;set_item_limit(3);</code> with <code>$feed-&gt;set_item_limit($_POST['displayNumber']);</code>. There may be some extra tweaking beyond that, but the general idea is to pass the submitted value into SimplePie.</p><h5>jQuery</h5><p>Now that we have the form fully functional we can layer some JavaScript on top of it to make it a little more <em>nifty</em>. This is using the <a
rel="external" href="http://malsup.com/jquery/form/">jQuery Form Plugin</a>. It&#8217;s the first time I&#8217;ve really used this plugin, but it&#8217;s actually pretty nice and easy to use. I had some little quirks with it, but nothing too major.</p><pre><code>$(function() {

<span>/* If JavaScript is active create a link to slide the form out */</span>
$('#getform a').click(function(){
	$('#selection').slideToggle('slow');
	return false;
});<span>//</span>

<span>/* make it look like something happened */</span>
$('#selection, #reader').hide(); <span>//</span>
$('#reader').fadeIn('slow'); <span>//</span>

<span> /* bind form using 'ajaxForm' */</span>
$('#selection').ajaxForm(options); 

<span>/* Submit form via Ajax */</span>
var options = {
    target:       '#rssloader',
}; <span>//</span>

});<span>// End jQuery</span></code></pre><p
class="demo"><a
href="http://www.csskarma.com/articles/examples/simplepieloader/">View demo</a></p><p
class="demo"><a
href="http://www.csskarma.com/articles/examples/simplepieloader/simplepieloader.zip">Download demo files</a></p><p>Anyway, that&#8217;s all I have for today. It&#8217;s a pretty unpolished demo so let me know what you think, where I screwed up and if you can build on it and make it better. I&#8217;ll try to write some more soon.</p><h5>Ingredient list</h5><ul><li><a
rel="external" href="http://simplepie.org/downloads/">SimplePie</a></li><li><a
rel="external" href="http://css-tricks.com/video-screencasts/55-adding-rss-content-with-simplepie/">Starter files from CSS-Tricks</a></li><li><a
rel="external" href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js&amp;downloadBtn=">jQuery</a></li><li><a
rel="external" href="http://malsup.com/jquery/form/">jQuery Form Plugin</a></li><li><a
href="http://sharkcool.net" rel="external">Hill&#8217;s Brain</a></li><li>Coffee</li></ul> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/selectable-rss/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 7/14 queries in 0.035 seconds using disk: basic

Served from: www.csskarma.com @ 2012-02-09 16:21:59 -->
