manx Posted September 20, 2007 Share Posted September 20, 2007 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"); } } ?> Link to comment https://forums.phpfreaks.com/topic/70069-solved-rss-parser-code-help-needed-to-limit-of-items/ Share on other sites More sharing options...
BlueSkyIS Posted September 20, 2007 Share Posted September 20, 2007 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++) { Link to comment https://forums.phpfreaks.com/topic/70069-solved-rss-parser-code-help-needed-to-limit-of-items/#findComment-351877 Share on other sites More sharing options...
manx Posted September 21, 2007 Author Share Posted September 21, 2007 THANK YOU! Thank You!!! This is the line I used: for($i = 0; $i < 5;$i++) works perfect now. I truly appreciate your help! Link to comment https://forums.phpfreaks.com/topic/70069-solved-rss-parser-code-help-needed-to-limit-of-items/#findComment-352054 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 hit topic solved thanks Link to comment https://forums.phpfreaks.com/topic/70069-solved-rss-parser-code-help-needed-to-limit-of-items/#findComment-352055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.