Jump to content

Recommended Posts

Hi there,

 

I am wanting to use the existing code I have for displaying an RSS feed, however, I want to only display the last 3 items that were added to the feed on a particular page.  I have it working so that it will display every item in the rss file at present, but dont know how to only show the last 3 items... ie: itemid=4, itemid=5 and itemid=6.

 

here is my RSS xml code:

<?xml version = "1.0"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" 
"http://my.netscape.com/publish/formats/rss-0.91.dtd">

<rss version="0.91">
<channel>
<title>MANZ Torque Newsletters</title>
<link>http://www.manz.org.nz</link>
<description></description>
<language>en-us</language>


<item>
<itemid>1</itemid>
<title>March 2007</title>
<link>http://www.domain.com/documents/March07.pdf</link>
<description>Description... 
</description>
</item>

<item>
<itemid>2</itemid>
<title>June 2007</title>
<link>http://www.domain.com/documents/June07.pdf</link>
<description>Description...
</description>
</item>

<item>
<itemid>3</itemid>
<title>September 2007</title>
<link>http://www.domain.com/documents/September07.pdf</link>
<description>This is the description for September 2007.
</description>
</item>

<item>
<itemid>4</itemid>
<title>October 2007</title>
<link>http://www.domain.com/documents/September07.pdf</link>
<description>This is the description for October 2007.
</description>
</item>

<item>
<itemid>5</itemid>
<title>November 2007</title>
<link>http://www.domain.com/documents/September07.pdf</link>
<description>This is the description for November 2007.
</description>
</item>

<item>
<itemid>6</itemid>
<title>December 2007</title>
<link>http://www.domain.com/documents/September07.pdf</link>
<description>This is the description for December 2007.
</description>
</item>

</channel>
</rss>

 

The code I am using to parse and display these items is:

<?php

class xItem {
  var $xTitle;
  var $xLink;
  var $xDescription;
}

// general vars
$sTitle = "";
$sLink = "";
$sDescription = "";
$arItems = array();
$itemCount = 0;

// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = "http://www.domain.com/newsletters.rss";
// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = "Verdana, Arial, Helvetica, sans-serif";
$uFontSize = "2";
// ********* End User-Defined Vars **************

function startElement($parser, $name, $attrs) {
  global $curTag;

  $curTag .= "^$name";

}

function endElement($parser, $name) {
  global $curTag;

  $caret_pos = strrpos($curTag,'^');

  $curTag = substr($curTag,0,$caret_pos);

}

function characterData($parser, $data) { global $curTag; // get the Channel information first
  global $sTitle, $sLink, $sDescription;  
  $titleKey = "^RSS^CHANNEL^TITLE";
  $linkKey = "^RSS^CHANNEL^LINK";
  $descKey = "^RSS^CHANNEL^DESCRIPTION";
  if ($curTag == $titleKey) {
    $sTitle = $data;
  }
  elseif ($curTag == $linkKey) {
    $sLink = $data;
  }
  elseif ($curTag == $descKey) {
    $sDescription = $data;
  }

  // now get the items 
  global $arItems, $itemCount;
  $itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
  $itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
  $itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";

  if ($curTag == $itemTitleKey) {
    // make new xItem    
    $arItems[$itemCount] = new xItem();     

    // set new item object's properties    
    $arItems[$itemCount]->xTitle = $data;
  }
  elseif ($curTag == $itemLinkKey) {
    $arItems[$itemCount]->xLink = $data;
  }
  elseif ($curTag == $itemDescKey) {
    $arItems[$itemCount]->xDescription = $data;
    // increment item counter
    $itemCount++;
  }
}

// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
  die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
  if (!xml_parse($xml_parser, $data, feof($fp))) {
    die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
  }
}
xml_parser_free($xml_parser);


// write out the items
?>

	<table style="width: 80%; text-align: left;" align="center">

		<?php
		for ($i=0;$i<count($arItems);$i++) {
		  $txItem = $arItems[$i];
		?>
		<tr>
			<td><span style="font-size: large; font-weight: bold"><a href = "<?php echo($txItem->xLink); ?>">
			<?php echo($txItem->xTitle); ?></a></span><br />
			<?php echo($txItem->xDescription); ?><br />
			</td>
		</tr>
		<?php

		}
		?>
	</table>


 

What do I need to alter, to tell it to only show the last 3 posts??  ie: if there are 25 posts, it will only show itemid=23, itemid=24, itemid=25... if there are 9 posts it will show itemid=7, itemid=8, itemid=9  etc... so as posts are added to the feed and the itemid increases it still only shows the last 3??

 

Can anyone help??

 

Thanks in advance...

Chris

Link to comment
https://forums.phpfreaks.com/topic/83301-display-rss-feed/
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.