Jump to content

[SOLVED] RSS Parser Code Help Needed to Limit # of items.


manx

Recommended Posts

Hi!

 

Sorry My first post is a plea for help! 

 

I'm using an old RSS Parser Code on one of my sites -- it does fine with the exception I can't figure out how to limit the number of items it shows on my .html page(s). If the rss feed URL has 100 items available, it will show all of them.

 

I have tried changing all the "0" to "5" -- no luck.

The code is below -- Can anyone help me? Thank You Very Much!

 

<?php

/*

Created by Global Syndication's RSS Parser

http://www.globalsyndication.com/rss-parser

*/

set_time_limit(0);

 

$file = "FEED URL";

 

$rss_channel = array();

$currently_writing = "";

$main = "";

$item_counter = 0;

 

function startElement($parser, $name, $attrs) {

global $rss_channel, $currently_writing, $main;

switch($name) {

case "RSS":

case "RDF:RDF":

case "ITEMS":

$currently_writing = "";

break;

case "CHANNEL":

$main = "CHANNEL";

break;

case "IMAGE":

$main = "IMAGE";

$rss_channel["IMAGE"] = array();

break;

case "ITEM":

$main = "ITEMS";

break;

default:

$currently_writing = $name;

break;

}

}

 

function endElement($parser, $name) {

global $rss_channel, $currently_writing, $item_counter;

$currently_writing = "";

if ($name == "ITEM") {

$item_counter++;

}

}

 

function characterData($parser, $data) {

global $rss_channel, $currently_writing, $main, $item_counter;

if ($currently_writing != "") {

switch($main) {

case "CHANNEL":

if (isset($rss_channel[$currently_writing])) {

$rss_channel[$currently_writing] .= $data;

} else {

$rss_channel[$currently_writing] = $data;

}

break;

case "IMAGE":

if (isset($rss_channel[$main][$currently_writing])) {

$rss_channel[$main][$currently_writing] .= $data;

} else {

$rss_channel[$main][$currently_writing] = $data;

}

break;

case "ITEMS":

if (isset($rss_channel[$main][$item_counter][$currently_writing])) {

$rss_channel[$main][$item_counter][$currently_writing] .= $data;

} else {

$rss_channel[$main][$item_counter][$currently_writing] = $data;

}

break;

}

}

}

 

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startElement", "endElement");

xml_set_character_data_handler($xml_parser, "characterData");

if (!($fp = fopen($file, "r"))) {

die("could not open XML input");

}

 

while ($data = fread($fp, 4096)) {

if (!xml_parse($xml_parser, $data, feof($fp))) {

die(sprintf("XML error: %s at line %d",

xml_error_string(xml_get_error_code($xml_parser)),

xml_get_current_line_number($xml_parser)));

}

}

xml_parser_free($xml_parser);

 

 

if (isset($rss_channel["ITEMS"])) {

if (count($rss_channel["ITEMS"]) > 0) {

for($i = 0; $i < count($rss_channel["ITEMS"]);$i++) {

if (isset($rss_channel["ITEMS"][$i]["LINK"])) {

print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");

} else {

print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");

}

print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); }

} else {

print ("No News Found");

}

}

?>

 

This line is probably telling the code to display all items:

 

for($i = 0; $i < count($rss_channel["ITEMS"]);$i++) {

 

Try changing it to something like this, where $num_to_display == the number of items to display:

 

for($i = 0; $i < $num_to_display;$i++) {

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.