0

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?

3 Answers 3

3

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 />';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your help. It works as brilliant.
@mrfox, if this is the solution to your problem, please consider to upvote and mark this answer as correct :)
0

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 />';

Comments

0

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.