Click to See Complete Forum and Search --> : LImit number of items retured from RSS feed


etrader
January 18th, 2008, 11:19 PM
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.


<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', '&amp;', $link);
$description1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $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.****.com/ws/rssapi?FeedName=SearchResults&siteId=$country&language=$language&output=RSS20&catref=C5&sacqy=&sacur=0&from=R6&saobfmts=exsif&dfsp=1&sacqyop=ge&saslc=2&floc=1&sabfmts=0&saprclo=&saprchi=&saaff=afcj&ftrv=1&ftrt=1&fcl=3&frpp=50&afcj=$affiliateid&nojspr=y&satitle=$searchterm&afmp=&sacat=-1&saslop=1&fss=0","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>

PeejAvery
January 19th, 2008, 10:51 AM
The best way in this scenario is to set a variable for the limit in the endElement() function.

$show = 20; // set how many rss items you want to show

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $show;

$link1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $link);
$description1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $description);
if ($name == "ITEM") {
if($show < 20){ // check to see that the number is not exceeded
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link1),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",trim($description1));
}
$show++; // increment the current number shown

$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}

etrader
January 19th, 2008, 08:02 PM
Thanks for your reply.

I tried that code but it doesn't return any results.

If I change the code to the following it returns 1 result.


$show = 20; // set how many rss items you want to show

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $show;

$link1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $link);
$description1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $description);
if ($name == "ITEM") {
if($show == 20){ // check to see that the number is not exceeded
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link1),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",trim($description1));
}
$show++; // increment the current number shown

$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}


Any ideas how to make it return 20?

PeejAvery
January 19th, 2008, 08:20 PM
Yes, I made a very simple mistake. Take my first line and change the variable value from 20 to 0.

$show = 0;

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $show;

$link1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $link);
$description1 = preg_replace('/&(?![a-z]+;)/i', '&amp;', $description);
if ($name == "ITEM") {
if($show < 20){ // 20 is the number of items to show
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link1),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",trim($description1));
}
$show++; // increment the current number shown

$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}