By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Who Among You Dare To Answer My Question?

P: n/a
Posted this a while ago, never got so much as a peep. Please take a look
and give any thoughts.

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>
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <j_*******************@newsread1.news.pas.earthlin k.net>, Noyb wrote:
Posted this a while ago, never got so much as a peep. Please take a look
and give any thoughts.
A few days is not a while.
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?


You have a look in the PHP manual, and lookup control structures. I'm
pretty sure there will be something that allows you repeated 10 or less
times.

Another option would be to write an XSL that generates a new XML
document with maximum 10 items. Therefore you need to have a look at the XSL manual.

Happy coding..

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #2

P: n/a
> 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);


couldnt't you do something like OR $i <= '10' in the while statement and
incrememnt $i in your while statement
Jul 17 '05 #3

P: n/a
On Sat, 05 Jun 2004 07:53:51 GMT, Noyb <za*****@hotmail.com> wrote:
Posted this a while ago, never got so much as a peep. Please take a look
and give any thoughts.

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]


now Here's my slightly modified code :D ((attached as well so you don't have to cut n' paste))

<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$rsscount = 0;
$max_feeds_shown = false;
$feeds_to_show = 10;

$feedurl="http://rss.news.yahoo.com/rss/business";
function startElement($parser, $name, $attrs)
{
global $insideitem, $tag, $title, $description, $link, $max_feeds_shown;

if ($max_feeds_shown == true)
return; // already shown max # of items

if ($insideitem)
$tag = $name;
elseif ($name == "ITEM")
$insideitem = true;
}

function endElement($parser, $name)
{
global $insideitem, $tag, $title, $description, $link, $max_feeds_shown, $rsscount,
$feeds_to_show;
if ($name == "ITEM" && $max_feeds_shown == false)
{
//take out ' characters that were causing probs with tooltip javascript
//$description = str_replace("'","",$description);
// better yet, replace ' with \' so they don't cause problems
$description = str_replace('\'', '\\\'', trim($description));

printf("<dt><a href='%s' target='mtgnews' onmouseover=\"return overlib('%s')\"
onmouseout=\"return nd();\">%s</a></dt>\n",
trim($link), htmlspecialchars($description), htmlspecialchars(trim($title)));

$title = '';
$description = '';
$link = '';
$insideitem = false;
if (++$rsscount >= $feeds_to_show)
$max_feeds_shown = true;
}
}

function characterData($parser, $data)
{
global $insideitem, $tag, $title, $description, $link;
if ($insideitem)
{
switch (strtoupper($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))
{
if ($max_feeds_shown)
break; // we have shown $feeds_to_show items..

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);

?>

Jul 17 '05 #4

P: n/a
Shane Lahey wrote:
On Sat, 05 Jun 2004 07:53:51 GMT, Noyb <za*****@hotmail.com> wrote:

Posted this a while ago, never got so much as a peep. Please take a look
and give any thoughts.

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]

now Here's my slightly modified code :D ((attached as well so you don't have to cut n' paste))

<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$rsscount = 0;
$max_feeds_shown = false;
$feeds_to_show = 10;

$feedurl="http://rss.news.yahoo.com/rss/business";
function startElement($parser, $name, $attrs)
{
global $insideitem, $tag, $title, $description, $link, $max_feeds_shown;

if ($max_feeds_shown == true)
return; // already shown max # of items

if ($insideitem)
$tag = $name;
elseif ($name == "ITEM")
$insideitem = true;
}

function endElement($parser, $name)
{
global $insideitem, $tag, $title, $description, $link, $max_feeds_shown, $rsscount,
$feeds_to_show;
if ($name == "ITEM" && $max_feeds_shown == false)
{
//take out ' characters that were causing probs with tooltip javascript
//$description = str_replace("'","",$description);
// better yet, replace ' with \' so they don't cause problems
$description = str_replace('\'', '\\\'', trim($description));

printf("<dt><a href='%s' target='mtgnews' onmouseover=\"return overlib('%s')\"
onmouseout=\"return nd();\">%s</a></dt>\n",
trim($link), htmlspecialchars($description), htmlspecialchars(trim($title)));

$title = '';
$description = '';
$link = '';
$insideitem = false;
if (++$rsscount >= $feeds_to_show)
$max_feeds_shown = true;
}
}

function characterData($parser, $data)
{
global $insideitem, $tag, $title, $description, $link;
if ($insideitem)
{
switch (strtoupper($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))
{
if ($max_feeds_shown)
break; // we have shown $feeds_to_show items..

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);

?>


Thanks Shane, I was trying the test down in the while loop and was stumped.
Steve.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.