Jump to content

[SOLVED] RSS Only displaying 3


mme

Recommended Posts

Hi

 

I would like to make it only display 3 entries how would I do this?

 

 <?php
    require_once 'rss/rss_fetch.inc';

    $url = 'http://mysite.com/rss' ;
    $rss = fetch_rss($url);

    if ( $rss and !$rss->ERROR) {
    foreach ($rss->items as $item ) {
    echo ' <p><a href="' . $item[link] . '" target="_blank"><b>' . $item[title] . '</b></a><br / >';
    echo '<i>Publish Date: ' . $item[pubdate] . '</i><br / >';
    echo $item[ description ] . ' </p>' ;
    }
    } else {
    echo '<b>RSS Error: ' . $rss->ERROR . ' </b><br / ><br />' ;
    }
    ?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/135564-solved-rss-only-displaying-3/
Share on other sites

<?php
    require_once 'rss/rss_fetch.inc';

    $url = 'http://mysite.com/rss' ;
    $rss = fetch_rss($url);

    if ( $rss and !$rss->ERROR) {
    $i=0;
    foreach ($rss->items as $item ) {
     if ($i > 2) 
         break;

    echo ' <p><a href="' . $item[link] . '" target="_blank"><b>' . $item[title] . '</b></a><br / >';
    echo '<i>Publish Date: ' . $item[pubdate] . '</i><br / >';
    echo $item[ description ] . ' </p>' ;
     $i++;
    }
    } else {
    echo '<b>RSS Error: ' . $rss->ERROR . ' </b><br / ><br />' ;

    }
    ?>

 

Should work.

 

Another variation is:

<?php
    require_once 'rss/rss_fetch.inc';

    $url = 'http://mysite.com/rss' ;
    $rss = fetch_rss($url);

    if ( $rss and !$rss->ERROR) {
    for($i=0; $i < 3; $i++) {  
      $item = $rss->items[$i];
        echo ' <p><a href="' . $item[link] . '" target="_blank"><b>' . $item[title] . '</b></a><br / >';
        echo '<i>Publish Date: ' . $item[pubdate] . '</i><br / >';
        echo $item[ description ] . ' </p>' ;
    
    }
    } else {
    echo '<b>RSS Error: ' . $rss->ERROR . ' </b><br / ><br />' ;

    }
    ?>

 

That might be more preferred but yea.

<?php
    require_once 'rss/rss_fetch.inc';

    $url = 'http://mysite.com/rss' ;
    $rss = fetch_rss($url);

    if ( $rss and !$rss->ERROR) {
        for ($x = 0; $x < 3; $x++) {
            $item = $rss->items[$x];
            echo ' <p><a href="' . $item[link] . '" target="_blank"><b>' . $item[title] . '</b></a><br / >';
            echo '<i>Publish Date: ' . $item[pubdate] . '</i><br / >';
            echo $item[ description ] . ' </p>' ;
    }
    } else {
    echo '<b>RSS Error: ' . $rss->ERROR . ' </b><br / ><br />' ;
    }
    ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.