doubleJ Posted September 10, 2011 Share Posted September 10, 2011 Hello... I'm working on a webpage (http://66.119.8.164/freedownloads.php) that pulls from an rss feed (http://www.moorelife.org/rss/FreeDownloads.xml). Please keep in mind that I'm working on content, not design. It's a very long page, currently. I want to have links on a sidebar that will allow sorting and filtering, based on <category>. I got the idea from, get this, Internet Explorer's built-in rss reader. It's great. I have a section of the page populating with SimpleXMLElement (that's where the case cover images and titles are). Above that is an alphabetic listing of all those entries populated by SimpleDOM's sortedXPath. Between those two are a few category links that use ajax to overwrite the section pulled with SimpleXMLElement. Now, I don't have any idea if I'm even doing this in an intelligent manner. As a matter of fact, I'm sure it's very far away from resourceful code. It's just what I have gotten to work when other options didn't work. Now, on to the problem. The code, below, is the php file (http://66.119.8.164/include/getrss.php) that ajax uses to overwrite the original list. <?php //get the category from URL $category = $_GET["category"]; $xml = "http://www.moorelife.org/rss/FreeDownloads.xml"; $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); $item = $xmlDoc->getElementsByTagName('item'); for ($i=0; $i<=5; $i++) { $item_title = $item->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue; $item_category = $item->item($i)->getElementsByTagName('category')->item(0)->childNodes->item(0)->nodeValue; echo "<p>".$item_title."</p>"; echo "<p>".$item_category."</p>"; } ?> How do I only show the entries where $catagory match <category>? JJ Quote Link to comment https://forums.phpfreaks.com/topic/246871-filter-rss-like-internet-explorer-does/ Share on other sites More sharing options...
doubleJ Posted September 10, 2011 Author Share Posted September 10, 2011 Well, I'm not sure what happened. I tried one last bit of code that I was working on, hours ago, and it works perfectly. I'm even re-using the SimpleXMLElement setup, instead of DOMDocument. I don't know why it's working, now, when it wasn't working, earlier. I don't particularly care, though. I think I'll post all the code, for posterity. I know I've been scouring the internet for hours trying to get this all to work. As I post, please, feel free to critique my code. I'm not a php guru, by any stretch. JJ Quote Link to comment https://forums.phpfreaks.com/topic/246871-filter-rss-like-internet-explorer-does/#findComment-1267848 Share on other sites More sharing options...
doubleJ Posted September 10, 2011 Author Share Posted September 10, 2011 freedownloads.php <?php // maybe find a way to do this with SimpleXMLElement include_once("include/SimpleDOM.php"); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="alternate" type="application/rss+xml" title="Free Downloads" href="http://www.moorelife.org/rss/FreeDownloads.xml" /> </head> <body> <div id="content" role="main"> <h1 id="title">Free Downloads</h1> <section id="series"> <h2>Moore Life Ministries series</h2> <nav> <h3>Alphabetical series links</h3> <ol> <?php // requires the loading of SimpleDOM (see beginning of file) $rss = simpledom_load_file("http://www.moorelife.org/rss/FreeDownloads.xml"); // sorts the array alphabetically (note you can use SORT_ASC or SORT_DESC as a third option foreach($rss->sortedXPath('channel/item', 'title') as $item) { $nospacetitle = basename($item->guid,".xml"); echo <<<END <li><a href="#$nospacetitle">$item->title</a></li>\n END; } ?> </ol> </nav> <aside> <script type="text/javascript"> function showRSS(str) { if (str.length==0) { document.getElementById("articles").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("articles").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","include/getrss2.php?category="+str,true); xmlhttp.send(); } </script> <a href="javascript:location.reload()">Show all</a> <a href="javascript:showRSS('Faith')">Faith</a> <a href="javascript:showRSS('Marriage')">Marriage</a> <a href="javascript:showRSS('Victory')">Victory</a> </aside> <div id="articles"> <?php // SimpleXMLElement requires php5 $rss = new SimpleXMLElement("http://www.moorelife.org/rss/FreeDownloads.xml", null, true); // if I can figure out how to get if ($category) else working // foreach($rss->xpath("channel/item[category='$category']") as $item) { foreach($rss->xpath("channel/item") as $item) { $filename = basename($item->guid,".xml"); // required for reading namespace parts // reference namespace parts by $ns_mlm->partname $ns_mlm = $item->children("http://www.moorelife.org/"); $address = explode(", ", $ns_mlm->address); $city = $address[0]; $state = $address[1]; $iso8601 = date("c", strtotime($item->pubDate)); $date = date("l, F d, Y", strtotime($item->pubDate)); echo <<<END <article id="$filename" class="vevent"> <img src="$ns_mlm->thumbnail" alt="" title="$item->title" /> <hgroup> <h3><a class="summary url" href="http://$fqdn/rss/$filename.xml">$item->title</a></h3> <h4>$ns_mlm->subtitle</h4> </hgroup> <p>Series started: <time class="dtstart" pubDate datetime="$iso8601">$date</time></p> <div class="location vcard"> <p class="fn org">$ns_mlm->location</p> <p class="adr"><span class="locality">$city</span>, <span class="region">$state</span></p> </div> <p><strong>$ns_mlm->availability</strong></p> </article>\n END; } ?> </div> </section> </div> <!-- end content --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/246871-filter-rss-like-internet-explorer-does/#findComment-1267855 Share on other sites More sharing options...
doubleJ Posted September 10, 2011 Author Share Posted September 10, 2011 include/getrss2.php Note that this is almost exactly like the code in freedownloads.php. There's probably a good way to combine the two code bases. I was thinking about having all the <div id="articles"> code in getrss2.php and then just include_once("getrss2.php"), but I couldn't figure out how to get it to load all the records or just the $category records. I might start working on that, again, now that this is working. JJ <?php $category = $_GET["category"]; // php5 required for SimpleXMLElement $rss = new SimpleXMLElement("http://www.moorelife.org/rss/FreeDownloads.xml", null, true); // step through all the items of the xml file // reference parts by $item->partname foreach($rss->xpath("channel/item[category='$category']") as $item) { Quote Link to comment https://forums.phpfreaks.com/topic/246871-filter-rss-like-internet-explorer-does/#findComment-1267858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.