TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   what a headache (http://www.talkphp.com/absolute-beginners/2746-what-headache.html)

julianochrist 05-02-2008 04:16 AM

what a headache
 
hello... here's the code
what i wanted to know is how do I list only the 5 first items from the xml? the foreach is taking all items from the xml code.


<?php
require_once "XML/RSS.php";

$rss =& new XML_RSS("sample.xml");
$rss->parse();

echo " <ul>\n";


foreach ($rss->getItems() as $item) {

echo "<li><a href=\"" . $item['link'] . "\">" . $item['title'] .
"</a></li>\n";

}

echo "</ul>\n";
?>

sjaq 05-02-2008 08:29 AM

PHP Code:

    $i 0;

    foreach (
$rss->getItems() as $item) {

        echo 
"<li><a href=\"" $item['link'] . "\">" $item['title'] . "</a></li>\n";
        
        ++
$i;
        
        if(
$i === 5) break;

    } 


Salathe 05-02-2008 03:24 PM

Alternatively you could only feed the first 5 items into the foreach.

PHP Code:

<?php
require_once "XML/RSS.php";

$rss =& new XML_RSS("sample.xml");
$rss->parse();

echo 
" <ul>\n";

// Use only the first 5 items
$items array_slice($rss->getItems(), 05);

foreach (
$items as $item) {

echo 
"<li><a href=\"" $item['link'] . "\">" $item['title'] .
"</a></li>\n";

}

echo 
"</ul>\n";
?>


julianochrist 05-03-2008 05:47 AM

i didnt get it... so $rss->items() return an array?

Garrett 05-03-2008 09:40 AM

Quote:

Originally Posted by julianochrist (Post 14193)
i didnt get it... so $rss->items() return an array?

I am sure if you look in the documentation or even the class itself they establish what it returns.

Aaron 05-03-2008 09:47 AM

Why not use a FOR loop?

Salathe 05-03-2008 01:35 PM

Quote:

Originally Posted by julianochrist (Post 14193)
i didnt get it... so $rss->items() return an array?

Yes, $rss->getItems() returns an array.

Quote:

Originally Posted by http://pear.php.net/package/XML_RSS/docs/latest/apidoc/XML_RSS/XML_RSS.html#methodgetItems
getItems [line 393]
array getItems( )

Get items from RSS file

This method returns an array containing the set of items that are provided by the RSS file.



All times are GMT. The time now is 11:54 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0