use a static counter.
for infor on static varaibles see
http://www.php.net/manual/en/languag...bles.scope.php
lets assume you the number of items you want to limit it to is in
$limit. so in your startElement() function
static $n = 0;
if($n++ < $limit) {
// main function processing goes here.
}
Noyb wrote:
[color=blue]
> I've set up a news links section using the RSS parsing tutorial found
> here:
http://www.sitepoint.com/article/560
> Everything works great but depending on the feed source there may be 3
> news links or 30. How can I limit the amount of titles, descriptions,
> and links output to be no more than 10?
> Thanks,
> Steve.
>
> [MY SLIGHTLY MODIFIED CODE PASTED BELOW]
>
>
> <dl>
> <?php
>
> $insideitem = false;
> $tag = "";
> $title = "";
> $description = "";
> $link = "";
> $feedurl="http://rss.news.yahoo.com/rss/business";
>
> 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") {
> $description=str_replace("'","",$description); //take out '
> characters that were causing probs with tooltip javascript
> printf("<dt><a href='%s' target='mtgnews' onmouseover=\"return
> overlib('%s')\" onmouseout=\"return nd();\">%s</a></dt>",
> trim($link),htmlspecialchars(trim($description)),h tmlspecialchars(trim($title)));
>
>
> $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");
> $fp = fopen($feedurl,"r")
> or die("Business News Unavailable.");
>
> while ($data = fread($fp, 4096))
> xml_parse($xml_parser, $data, feof($fp))
> 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);
>
> ?>
> </dl>[/color]
------------ And now a word from our sponsor ------------------
For a quality usenet news server, try DNEWS, easy to install,
fast, efficient and reliable. For home servers or carrier class
installations with millions of users it will allow you to grow!
---- See
http://netwinsite.com/sponsor/sponsor_dnews.htm ----