Reputation: 49
I have this code and it works great
$i = 0; // counter
$url = "http://www.banki.ru/xml/news.rss"; // example url to parse
$rss = simplexml_load_file($url); // XML parser
print '<h2><img style="vertical-align: middle;" src="'.$rss->channel->image->url.'" /> '.$rss->channel->title.'</h2>'; // channel title + img with src
foreach($rss->channel->item as $item) {
if ($i < 1) { // parse only 1 item
print '<a href="'.$item->link.'">'.$item->title.'</a><br />';
}
$i++;
}
But I would like to show only second item from this feed. How I can do this?
Upvotes: 0
Views: 178
Reputation: 136
Just change the if condition it will get you the second session
<?php
$i = 0; // counter
$url = "http://www.banki.ru/xml/news.rss"; // example url to parse
$rss = simplexml_load_file($url); // XML parser
print '<h2><img style="vertical-align: middle;" src="'.$rss->channel->image->url.'" /> '.$rss->channel->title.'</h2>'; // channel title + img with src
foreach($rss->channel->item as $item) {
if ($i > 1) { // parse only 1 item
print '<a href="'.$item->link.'">'.$item->title.'</a><br />';
}
$i++;
}
Upvotes: 0
Reputation: 2525
Change this
if ($i < 1) { // parse only 1 item
print '<a href="'.$item->link.'">'.$item->title.'</a><br />';
}
to
if ($i == 1) { // parse only 1 item
print '<a href="'.$item->link.'">'.$item->title.'</a><br />';
}
Upvotes: 3
Reputation: 8340
try:
$i = 0; // counter
$url = "http://www.banki.ru/xml/news.rss"; // example url to parse
$rss = simplexml_load_file($url); // XML parser
print '<h2><img style="vertical-align: middle;" src="'.$rss->channel->image->url.'" /> '.$rss->channel->title.'</h2>'; // channel title + img with src
print '<a href="'.$rss->channel->item[1]->link.'">'.$rss->channel->item[1]->title.'</a><br />';
Upvotes: 0