mightymaster Posted April 30, 2009 Share Posted April 30, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156288-rss-parser-need-to-add-limit-help/ Share on other sites More sharing options...
ignace Posted April 30, 2009 Share Posted April 30, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156288-rss-parser-need-to-add-limit-help/#findComment-822838 Share on other sites More sharing options...
mightymaster Posted April 30, 2009 Author Share Posted April 30, 2009 Ok ... That means what? LOL Sorry, relatively new to php. What I need is the parser to read the date,time, and title of the feed and publish it. With that I would like to limit the number of entries to 5-10. Any other script recommendations then? Quote Link to comment https://forums.phpfreaks.com/topic/156288-rss-parser-need-to-add-limit-help/#findComment-822841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.