Results 1 to 3 of 3

Thread: limit xml result

  1. #1
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default limit xml result

    Hello Good People,

    Please help me limit the entries generated on my site from a rss feed...
    The code below shows all the entries.

    Thankyou in advance!

    <?php
    $url = "http://news.google.com/?output=rss";
    $rss = simplexml_load_file($url);
    if($rss)
    {
    $items = $rss->channel->item;
    foreach($items as $item)
    {
    $title = $item->title;
    $link = $item->link;
    $description = $item->description;
    $image = $item->image;
    echo ' <div class="img rand" >
    <img src="'.$image.'" alt="'.$title.'">
    <a href="'.$link.'" target="_blank">
    <img src="/overlay.gif" class="cvr" alt="'.$title.'"/></a>
    <div class="desc truncate" >'.$title.'</div>
    </div>';
    }
    }
    ?>

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Limit the results to how many?

    You can use a simple counter:

    PHP Code:
    <?php
    $url 
    "http://news.google.com/?output=rss";
    $rss simplexml_load_file($url);
    $limit 10// Change this number to however many items you want to display
    $counter 1;
    if(
    $rss)
    {
    $items $rss->channel->item;
    foreach(
    $items as $item)
    {
    if(
    $counter <= $limit) {
    $title $item->title;
    $link $item->link;
    $description $item->description;
    $image $item->image;
    echo 
    ' <div class="img rand" >
    <img src="'
    .$image.'" alt="'.$title.'">
    <a href="'
    .$link.'" target="_blank">
    <img src="/overlay.gif" class="cvr" alt="'
    .$title.'"/></a>
    <div class="desc truncate" >'
    .$title.'</div>
    </div>'
    ;
    $counter++;
    }
    }
    }
    ?>

  3. #3
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Thank You Sir for your help! No doubt it works and I know this thread will be helpful to others as well!

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
  •