How to limit the number of news in a PHP RSS reader?

Discussion in 'PHP' started by franck~, Feb 16, 2006.

  1. #1
    I'm trying to use the code in this RSS parser tutorial:
    http://www.sitepoint.com/article/php-xml-parsing-rss-1-0/4

    I have already succeeded in adding the functionality of shortening the news DESCRIPTION to the first x words and then add "...".

    Now how do you add the option to limit the number of news shown? Say, show only 3 news max.
     
    franck~, Feb 16, 2006 IP
  2. hTK

    hTK Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ah.. that looks a bit confusing,
    This is jsut a suggestion>

    I prefer to use lastrss from lastrss.webdot.cz

    It's (GNU/GPL) and has variables you can set the value of to limit the number of RSS items,
    I'm sure limiting the string length is easily accomplished.

    Good Luck'
     
    hTK, Feb 16, 2006 IP
  3. wasabi

    wasabi Guest

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    MagpieRSS for me, very simple to set up and use.

    Try replacing the while by a for?
     
    wasabi, Feb 16, 2006 IP
  4. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What I like in the sitepoint rss reader is that everything is in one small file. It's convenient for me because it can be included anywhere in a site very easily.
    I can just do something like:
    
    <div><?
    include "http://www.domain.com/RSS_reader.php";
    ?></div>
    
    PHP:
    Since it's just for use on my website to display my own feed, I don't need more than that.

    To set a limit to the number of news I was told to "set up a counter variable in the PHP
    code, incrementing it each time endElement outputs an item.
    endElement could then be written to only produce output if that counter is below the desired limit."

    But I don't know how to do that, all my attempts had no success.:(
     
    franck~, Feb 17, 2006 IP
  5. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, I got the solution by adding a counter to endElement:
    (If anybody is interested)

    
    class RSSParser {
    
       var $itemCounter = 5; // number of items to show
       var $insideitem = false;
    
    PHP:
    and integration:
    
    function endElement($parser, $tagName) {
              if ($tagName == "ITEM") {
                 if($this->itemCounter-- < 1) {return;}
    
    PHP:
    Ity works great, and it keeps the code simple. The whole RSS reader is 64 lines of code in ONE file!
    :)
     
    franck~, Feb 18, 2006 IP
  6. wasabi

    wasabi Guest

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Good job !
    But does it have cache??
    Because the loading time will increase significantly if you have lots of items to show.
     
    wasabi, Feb 18, 2006 IP
  7. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ha, you got a point here: no cache.
    But as I said, it's only for a specific use on a site and there will never be more than 3 short items to show.
    (They are shortened by the addition of a function to limit the number of words par items.)
     
    franck~, Feb 18, 2006 IP
  8. smarty

    smarty Banned

    Messages:
    196
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hello all

    m also using "lastrss" . Using my feeds from 2 blogs. now everything was set except that. i want to limit the characters shown on description.

    can anyone tell HOW TO LIMIT THE CHARACTERS IN DESCRIPTION ?????


    Thanks in advance.!
     
    smarty, May 8, 2007 IP
  9. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    
    <?php
    
    function summary($content, $limit){
    
               
    
        $content = explode(' ',$content);
    
        for($i=0; $i<$limit; $i++){
    
            $summary[$i] = $content[$i];
    
        }
    
        $summary = implode(' ', $summary).'..';
    
    
    
    return $summary;
    
    }
    
    
    
    $content = 'This is some demo content. You can put all you want in here, 
    but only the first 3 words will be returned.';
    
    
    
    echo summary($content,3); // assign the content and limit
    
    ?>
    
    PHP:
     
    franck~, May 13, 2007 IP