RSS Parser Code Help Needed to Limit # of items.

Discussion in 'PHP' started by manx, Sep 20, 2007.

  1. #1
    Hi!

    I'm using an old RSS Parser Code on one of my sites -- it does fine with the exception it 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? Thanks!

    <?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");
    }
    }
    ?>
     
    manx, Sep 20, 2007 IP
  2. manx

    manx Active Member

    Messages:
    293
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Answer Found --

    A kind person at http://phpfreaks.com forum told me to replace
    this line of code:

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

    with:

    for($i = 0; $i < 5;$i++)

    --"5" being the number of items I wish to display.

    The RSS Parser Code works perfectly now.

    BTW -- This is a great RSS Parser Code.

    I have it on my .html pages and added the .php allowed line
    in my .htaccess file.

    Brings up the RSS feeds. :)
     
    manx, Sep 20, 2007 IP
  3. msaqibansari

    msaqibansari Peon

    Messages:
    84
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Replace following line
    for($i = 0; $i < count($rss_channel["ITEMS"]);$i++) {

    with this code
    $maxcounter = count($rss_channel["ITEMS"];
    if ($maxcounter > 50)
    $maxcounter = 50;
    for($i = 0; $i < $maxcounter;$i++) {
     
    msaqibansari, Sep 20, 2007 IP