homepage Welcome to WebmasterWorld Guest from 54.205.241.107
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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



 
Msg#: 4361035 posted 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

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4361035 posted 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#: 4361035 posted 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#: 4361035 posted 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#: 4361035 posted 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved