TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-02-2008, 04:16 AM   #1 (permalink)
The Visitor
 
julianochrist's Avatar
 
Join Date: Apr 2008
Posts: 3
Thanks: 3
julianochrist is on a distinguished road
Confused 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";
?>
julianochrist is offline  
Reply With Quote
Old 05-02-2008, 08:29 AM   #2 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

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;

    } 
sjaq is offline  
Reply With Quote
The Following User Says Thank You to sjaq For This Useful Post:
julianochrist (05-02-2008)
Old 05-02-2008, 03:24 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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";
?>
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
julianochrist (05-03-2008)
Old 05-03-2008, 05:47 AM   #4 (permalink)
The Visitor
 
julianochrist's Avatar
 
Join Date: Apr 2008
Posts: 3
Thanks: 3
julianochrist is on a distinguished road
Default

i didnt get it... so $rss->items() return an array?
julianochrist is offline  
Reply With Quote
Old 05-03-2008, 09:40 AM   #5 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 6
Thanks: 0
Garrett is on a distinguished road
Default

Quote:
Originally Posted by julianochrist View Post
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.
Garrett is offline  
Reply With Quote
Old 05-03-2008, 09:47 AM   #6 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Why not use a FOR loop?
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 05-03-2008, 01:35 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by julianochrist View Post
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.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
julianochrist (05-04-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:39 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design