SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimplePie RSS question

    I am using SimplePie to display my latest Facebook fan age updates. All works great, however, I cannot get it to limit the list to 1 item.

    Here is the code I am using:

    PHP Code:
    <?php
        
    require_once('rss/rss_fetch.inc');
        
    $rss fetch_rss('http://www.facebook.com/feeds/page.php?id=284635508257220&format=rss20');
    if(isset(
    $_GET['item']))
    {
            
    $start $_GET['item'];
            
    $length 1;
    }
        echo 
    "<a href=".$rss->channel['link']."><strong>".$rss->channel'title']."</strong></a>";
        foreach (
    $rss->get_items(01) as $item){
        
    $href $item['link'];
        
    $title $item['title'];
        
    //$desc = $item['description'];
        
    echo "<p><a href=$href>$title</a></p>";
        if(
    $desc)
        echo 
    $desc;
        }
        
    ?>
    Anyone know what I am doing wrong?


    Geoserv.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Two ways:

    Only display the first item in the array (0).


    PHP Code:


        $items 
    $rss->get_items(01); 

        
    // items is now the array of results


        // access only the first one

        
    $href $items[0]['link']; 
        
    $title $items[0]['title']; 
        
    // etc
        
    ?> 
    Or

    Set a counter in case you ever want to change that number from 1 to 2.

    PHP Code:

    $ctr
    =0;  // set a counter to 0
    $target // set a target number, initially in your case: 1

    foreach ($rss->get_items(01) as $item){ 

        if( 
    $ctr $target ) {  // if the counter is less that your target, go ahead

           
    $href $item['link']; 
           
    $title $item['title']; 
           
    //$desc = $item['description']; 
           
    echo "<p><a href=$href>$title</a></p>"
           if(
    $desc
           echo 
    $desc

           }
        
    $ctr++;  // iterate the counter
        

        
    ?> 
    That is not particularly efficient or well written, but should suffice to show you how to limit your array iterations.

    You could also use a while ($ctr < $target) loop for example.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •