LImit number of items retured from RSS feed

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
etrader1
Forum Newbie
Posts: 1
Joined: Fri Jan 18, 2008 10:24 pm

LImit number of items retured from RSS feed

Post by etrader1 »

Can someone please show me how to limit the number of item returned from an RSS feed. At the moment, my script returns 100 results but I only want it to return 20.

Code: Select all

 
<dl>
<?php
 
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
 
function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
        $tag = $name;
    } elseif ($name == "ITEM") {
        $insideitem = true;
    }
}
 
function endElement($parser, $name) {
    global $insideitem, $tag, $title, $description, $link;
    
    $link1 = preg_replace('/&(?![a-z]+;)/i', '&', $link); 
    $description1 = preg_replace('/&(?![a-z]+;)/i', '&', $description); 
    if ($name == "ITEM") {
        printf("<dt><b><a href='%s'>%s</a></b></dt>",
            trim($link1),htmlspecialchars(trim($title)));
        printf("<dd>%s</dd>",trim($description1));
        $title = "";
        $description = "";
        $link = "";
        $insideitem = false;
    }
}
 
function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    switch ($tag) {
        case "TITLE":
        $title .= $data;
        break;
        case "DESCRIPTION":
        $description .= $data;
        break;
        case "LINK":
        $link .= $data;
        break;
    }
    }
}
 
// Create an XML parser
$xml_parser = xml_parser_create();
 
// Set the functions to handle opening and closing tags 
xml_set_element_handler($xml_parser, "startElement", "endElement");
 
// Set the function to handle blocks of character data 
xml_set_character_data_handler($xml_parser, "characterData");
 
$query = "SELECT country,language,affiliateid FROM config";
$result = mysql_query($query)
or die ("Error processing your request.");
while ( $row = mysql_fetch_array($result))
{ 
extract($row);
}
 
$searchterm = preg_replace("/\s/", "%20", $searchterm);
 
// Open the XML file for reading 
$fp = fopen("http://rss.api.ebay.com/","r")
    or die("Error reading RSS data.");
    
// Read the XML file 4KB at a time 
while ($data = fread($fp, 4096))
 
 
// Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser, $data, feof($fp))
 
// Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d", 
            xml_error_string(xml_get_error_code($xml_parser)), 
            xml_get_current_line_number($xml_parser)));
 
// Close the XML file 
fclose($fp);
 
// Free up memory used by the XML parser 
xml_parser_free($xml_parser);
 
?>
</dl>
 
Post Reply