RSS feed: displayed feed adjustment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SCSI9
Forum Newbie
Posts: 1
Joined: Thu Jan 20, 2011 2:39 pm

RSS feed: displayed feed adjustment

Post by SCSI9 »

Hi,

I am using following code, which displays my forum feeds on my main page. The script displays last 15 post. I would like to limit the amount of post to 10. I have looked through the script, but I am not able to find where the script limits the output, up to 15 post. I am a PHP newbie and have no idea how to do it, could somebody help me?

Code: Select all

//rss

// ######################################################
// ## configuration
 // ##
 // ## $rss2_file= 'http://www.vbulletin.com/forum/external.php?type=rss2';
 // ## Adjust this variable to point to your RSS2 feed
  
 $rss2_file = 'http://www.voetbaldivisie.nl/voetbalforum/external.php?type=RSS2';
     
 // ## configuration end
 // ######################################################
 // ## Do not touch code below!
     
     
 $is_item = false;
 $tag = '';
 $title = '';
 $description = '';
 $link = '';
 $date = '';
 $author = '';
     
 function character_data($parser, $data)
 {
     global $is_item, $tag, $title, $description, $link, $date, $author;
     if ($is_item)
     {
         switch ($tag)
         {
             case "TITLE":
             $title .= $data;
             break; 
     
             //case "DESCRIPTION":
             //$description .= $data;
             //break;
     
             case "LINK":
             $link .= $data;
             break;
     
             //case "PUBDATE":
             //$date .= $data;
             //break;
     
             //case "AUTHOR":
             //$author .= $data;
             //break;
         }
     }
 }
     
 function begin_element($parser, $name)
 {
     global $is_item, $tag;
     if ($is_item)
     {
         $tag = $name;
     }
     else if ($name == "ITEM")
     {
         $is_item = true;
     }
 }
     
 function end_element($parser, $name)
 {
     global $is_item, $title, $description, $link, $date, $author, $rss2_output;
     if ($name == "ITEM")
     {
         $rss2_output .= "<dt>&nbsp;&nbsp;&nbsp;<strong><a href='" . trim($link) . "'>" . htmlspecialchars(trim($title)) . "</a></strong>" . htmlspecialchars(trim($date)) . "<em><br><br>" . htmlspecialchars(trim($author)) . "</em></dt><dd>" . htmlspecialchars(trim($description)) . "</dd>";
         $title = "";
         $description = "";
         $link = "";
         $date = "";
         $author = "";
         $is_item = false;
     }
 }
     
     
 $parser = xml_parser_create();
     
 xml_set_element_handler($parser, "begin_element", "end_element");
 xml_set_character_data_handler($parser, "character_data");
 $fp = fopen($rss2_file,"r");
     
 while ($data = fread($fp, 4096))
 {
     xml_parse($parser, $data, feof($fp));        
 }
     
 fclose($fp);
 xml_parser_free($parser);

//end rss code
output:

Code: Select all

<?php echo $rss2_output; ?>
The feed is coming from a vBulletin forum.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: RSS feed: displayed feed adjustment

Post by John Cartwright »

It is because you are loading the XML file in bytes. I would simply load the XML in simplexml, and iterate the item rows using a simple foreach() loop, and increment a variable on each iteration of the loop, then check if the variable is 10 or more, and if so break; the loop.

I'll leave it for you to try, and post back if you get stuck.

And theres always an option to use one of the million RSS parsers on the interwebs.


EDIT | The nasty solution would be to define a counting variable in the global scope (anywhere outside those functions), then declare it global in end_element() function, then change

if ($name == "ITEM")

to something like

if ($name == "ITEM" && $your_global_counting_variable <= 10)

The only reason I provided a nasty solution is because it will prevent the entire XML document from being downloaded, which is what I assume your trying to do with this awkward implementation.
Post Reply