PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
becky-atlanta
Forum Commoner
Posts: 74 Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA
Post
by becky-atlanta » Sat May 23, 2009 9:28 am
I found this neat script to extract RSS feed:
Code: Select all
<?php
$doc = new DOMDocument();
$doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
?>
I am able to use it to get the feed. Now I am trying to get the first article only. I tried:
Code: Select all
echo $itemRSS["title"];
echo $itemRSS["desc"];
echo $itemRSS["link"];
echo $itemRSS["date"];
but I get the last item, not the first one that I want.
I am still very confused with array. Can you give me a hand?
Last edited by
Benjamin on Tue May 26, 2009 10:16 am, edited 1 time in total.
Reason: Changed code type from text to php.
divito
Forum Commoner
Posts: 89 Joined: Sun Feb 22, 2009 7:29 am
Post
by divito » Sat May 23, 2009 10:26 am
Code: Select all
<?php
$doc = new DOMDocument();
$doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
krsort($arrFeeds);
?>
becky-atlanta
Forum Commoner
Posts: 74 Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA
Post
by becky-atlanta » Sat May 23, 2009 11:38 am
Thanks for your help. However, it is not working. It is still giving me the last item.
divito
Forum Commoner
Posts: 89 Joined: Sun Feb 22, 2009 7:29 am
Post
by divito » Sat May 23, 2009 1:24 pm
Can we see the next bit of code after the sort?
Scriptor
Forum Newbie
Posts: 9 Joined: Fri May 22, 2009 12:27 am
Post
by Scriptor » Sat May 23, 2009 3:01 pm
that will show first item of the array.
or if you want just 1 single array you can always just pull it out...
Last edited by
Benjamin on Tue May 26, 2009 10:16 am, edited 1 time in total.
Reason: Changed code type from text to php.
becky-atlanta
Forum Commoner
Posts: 74 Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA
Post
by becky-atlanta » Sat May 23, 2009 5:45 pm
divito wrote: Can we see the next bit of code after the sort?
After the reverse sort, I just simply used the code I was using to test:
Code: Select all
echo $itemRSS["title"];
echo $itemRSS["desc"];
echo $itemRSS["link"];
echo $itemRSS["date"];
and it returned the same item I was getting before the reverse sort.
Scriptor wrote: echo $array[0];
I tried:
Code: Select all
echo $arrFeeds[0]; \* it output "Array".
I tried:
Code: Select all
echo $itemRSS[0]; \* it output nothing.
Last edited by
Benjamin on Tue May 26, 2009 10:17 am, edited 1 time in total.
Reason: Added [code=php] tags.
divito
Forum Commoner
Posts: 89 Joined: Sun Feb 22, 2009 7:29 am
Post
by divito » Sat May 23, 2009 5:57 pm
Try this:
Code: Select all
<?php
$doc = new DOMDocument();
$doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
krsort($arrFeeds);
foreach ($arrFeeds as $entry) {
echo $entry["title"];
echo $entry["desc"];
echo $entry["link"];
echo $entry["date"];
}
?>
If it works, just throw in a counter and you should be set, assuming that helps in the way you're deploying this.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat May 23, 2009 6:05 pm
Like Scriptor said three hours ago, the first item in an array is
If you want the first
chronologically then it'll be the last item the code processed (since RSS lists newest -> oldest).
becky-atlanta
Forum Commoner
Posts: 74 Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA
Post
by becky-atlanta » Sat May 23, 2009 8:39 pm
Thank you for everyone's tips. Now I learned some new codes I did not know before.
Here's how I make it work:
Code: Select all
<?php
$doc = new DOMDocument();
$doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
$arrFeeds = array();
$i = 0; [color=#FF0000] //added to get the first item only//[/color]
foreach ($doc->getElementsByTagName('item') as $node) {
if (++$i == 1){ [color=#FF0000]//added to get the first item only//[/color]
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
echo "<a href=\"";
echo $itemRSS["link"];
echo "\"><h1>";
echo $itemRSS["title"];
echo "</h1></a>";
echo "<br>";
echo $itemRSS["desc"];
echo "<br>";
echo $itemRSS["date"];
echo "<br>";
break;} [color=#FF0000]//added to get the first item only//[/color]
}
?>
Here is a good page to explain different ways to get the first element of an array,
http://slavi.biz/blog/how-to-get-the-fi ... 01-10.html
Last edited by
Benjamin on Tue May 26, 2009 10:18 am, edited 1 time in total.
Reason: Changed code type from text to php.