473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Who Among You Dare To Answer My Question?

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
4 1835
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TimS | last post by:
I am getting a baffling File not found error (0x800A0035). I am writing asp on a windows 2000 server. I build a path and filename from several fields in a jet database using SQL commands, like...
19
by: Jordan | last post by:
Let me just start by saying I'm a very accomplished ASP programmer. I need to rely on that becuase this error boggles the mind. Just today, I had to troubleshoot an error in one of my...
11
by: (Pete Cresswell) | last post by:
I'd like to avoid coding a long set of IF... OR IF...statements. I can do a Select Case, but that would only let me case out on a single value. What I'm looking for is something like If...
7
by: Nadia | last post by:
If that project is not yours and you only have to compile it using makfile .Any help is greatly appreciated. Thanks,
18
by: jess.austin | last post by:
hi, This seems like a difficult question to answer through testing, so I'm hoping that someone will just know... Suppose I have the following generator, g: def f() i = 0 while True: yield...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.