| Get First Instance Of Array Element Echo only 1 of each <catagory> in rss feed |
doubleJ

msg:4361037 | 2:27 am on Sep 11, 2011 (gmt 0) | I have an rss feed with multiple categories. I created a list of links, manually, but I'm trying to automate it. This code pulls all of the categories, but it displays every instance of each.
<ul> <?php $rss = simpledom_load_file("http://www.moorelife.org/rss/FreeDownloads.xml"); foreach($rss->sortedXPath('channel/item', 'category') as $item) { $category = basename($item->category); echo <<<END <li><a href="javascript:showRSS('$category')">$category</a></li> END; } ?> </ul>
I assume there's an easy way to pull just the first instance, but I haven't gotten it. I tried for(), while(), and using an array variable. I probably did it wrong, though. Hehehe... JJ
|
penders

msg:4361075 | 10:18 am on Sep 11, 2011 (gmt 0) | To get just the first item you wouldn't need any looping structure, since you don't want to loop through anything, so something like this might work... $item = reset($rss->sortedXPath('channel/item', 'category')); Or, with your loop, you could just break at the end. So only 1 iteration of the loop occurs?!
|
doubleJ

msg:4361159 | 7:12 pm on Sep 11, 2011 (gmt 0) | I'm not looking for just 1 item, but only 1 instance of each category. The whole file would have to be passed through in order to make sure each category is found. I'll try your break idea, though. JJ Edit... Yeah, break showed only 1 result, instead of 10 or so. JJ
|
doubleJ

msg:4361212 | 11:16 pm on Sep 11, 2011 (gmt 0) | Ok... I've been able to pull the individual categories from the xml file.
<?php $categories = array(); foreach($rss->sortedXPath('channel/item', 'category') as $item) { $categories[] = basename($item->category); } $uniquecategories = array_unique($categories); echo "<pre>"; print_r($uniquecategories); echo "</pre>"; ?>
That code produces... Array ( [0] => Commitment [3] => Direction And The Plan Of God [15] => Faith [26] => Free From Fear [29] => Goodness Of God [36] => Healing [46] => Holy Spirit [53] => Honor [56] => Marriage [64] => Prosperity [79] => Spiritual Growth [104] => Uncategorized [112] => Victory ) |
| I have been trying to get array_unique($uniquecategories); to work, but have been unsuccessful. I tried...
$uniquecategories = array_unique($categories); for($i = 0; $i <= count($uniquecategories); $i++) { echo "<pre>"; print_r($uniquecategories[$i]); echo "</pre>"; }
But that code only produces... Commitment Direction And The Plan Of God |
| I'm not particularly sure why it's only showing 2 categories or why there's a space between them. JJ
|
doubleJ

msg:4361215 | 11:30 pm on Sep 11, 2011 (gmt 0) | Woohoo... I got it working!
<ul> <?php include_once("include/SimpleDOM.php"); $rss = simpledom_load_file("http://www.moorelife.org/rss/FreeDownloads.xml"); $categories = array(); foreach($rss->sortedXPath('channel/item', 'category') as $item) { $categories[] = basename($item->category); } $uniquecategories = array_unique($categories); foreach($uniquecategories as $key => $value) { echo <<<END <li><a href="javascript:showRSS('$value')">$value</a></li> END; } ?> </ul>
That code produces (with dots, as it's an unordered list)... Commitment Direction And The Plan Of God Faith Free From Fear Goodness Of God Healing Holy Spirit Honor Marriage Prosperity Spiritual Growth Uncategorized Victory |
| Feel free to critique my code. It might not be elegant, but it does what I want. Now, I want to try and figure out how to replace SimpleDOM and sortedXPath with regular SimpleXMLElement and sort(). JJ
|
|
|