Jump to content

RSS Parser - Need to add limit. HELP?


mightymaster

Recommended Posts

Currently using the following script on a site I am building for a local church. I need to know how to limit the number of entries shown. I only want it to pull like 5 - 10 entries. Right now it pulls them all I believe.

 

<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('http://spongecell.com/rss/events/TheOC/Upcoming+Events', 'http://spongecell.com/rss/events/TheOC/Upcoming+Events', 'http://spongecell.com/rss/events/TheOC/Upcoming+Events');
srand((float) microtime() * 10000000); // seed the random gen 
$random_key = array_rand($locations);
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;
if ($name == "ITEM") {
 echo "<dt><b><a href='upcoming_events.php' target='new'>" . trim($title) . "</a></b></dt>\n";
 echo "<br><br>\n";
 //printf("<dt><b><a href='%s' target=new>%s</a></b></dt>",trim($link),htmlspecialchars(trim($title)));
 //printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));
 $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;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $locations[$random_key]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

//$fp = fopen($locations[$random_key], 'r') or die("Error reading RSS data.");
// while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data) 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)));
//fclose($fp);
xml_parser_free($xml_parser);
?>

 

It also would be great to make the script list the full date. Right now is just lists "5/3". Would love it to list 05/03/09 or Sunday May 3rd. Ideas?

Link to comment
Share on other sites

You can not limit the amount of rss feeds read if using xml_*() functions. If you want to limit the amount of rss feeds read you need to first read the rss then loop over the entries and after each loop see if the limit has been reached.

 

<?php
// read the file:
$rssFeed = file_get_contents('../rss/feed.xml');
$xml = new SimpleXmlElement($rssFeed);
$counter = 0;
$limit = 10;
foreach ($xml as ..)
    ..
    $counter++;
    if ($counter === $limit) break;
}
?>

 

However there must be a way to only read the first x records instead of each and every entry

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.