A
A
Anton Kurilov2015-03-20 12:46:23
PHP
Anton Kurilov, 2015-03-20 12:46:23

How to limit access to RSS file?

There is a primitive PHP code that accesses the RSS file and takes the titles and links from there. It is necessary to make sure that the number of hits is limited (for example, to display 10 links). I know that counters are needed, but I don’t know where to implement them. Complete noob at PHP. It’s a shame to contact Freelancing with such a trifle.
I am attaching the code.

<?
$url = 'http://antonkurilov.ru/russian.xml';
$rss = simplexml_load_file($url);
foreach ($rss->channel->item as $item) {
echo '<a href="'.$item->guid.'" target="_blank">'.$item->title.'</a>'; 
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Mokhov, 2015-03-20
@antonkurilov

break - stops the execution of the cycle, therefore we consider the cycle when the required threshold is reached break;

$url = 'http://antonkurilov.ru/russian.xml';
$rss = simplexml_load_file($url);
$i = 1;
foreach ($rss->channel->item as $item) {
    echo '<a href="'.$item->guid.'" target="_blank">'.$item->title.'</a>'; 
    if (++$i > 10) break;
}

C
Centrino, 2015-03-20
@Centrino

$i = 0;
foreach ($rss->channel->item as $item) {
echo ' '.$item->title.' ';
$i++;
if ($i >10) {
break;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question