Jump to content

Recommended Posts

Hi there...

I am currently using an rss file to display news on my webpage.  I would like to change it a bit so that it only displays news that is not expired.

 

here is my rss file

<?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>My Title</title>
<link></link>
<description></description>
<language>en-us</language>


<item>
<itemid>1</itemid>
<title>Ground Breaking News</title>
<link>http://www.google.com</link>
<datePosted>22/01/2008</datePosted> <!-- displayed dd/mm/yyyy | Does not currently display in page-->
<dateExpired>22/01/2008</dateExpired> <!-- displayed dd/mm/yyyy| Does not currently display in page -->
<description>This is the description of this news article
</description>
</item>



</channel>
</rss>

 

 

and here is the code I use to currently display the data on my page

 

<?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.mydomain.com/3views.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
<?php
for ($i=0;$i<count($arItems);$i++) {
    $txItem = $arItems[$i];
?>
			<li><a href = "<?php echo($txItem->xLink); ?>">
			<?php echo($txItem->xTitle); ?></a><br />
			<?php echo($txItem->xDescription); ?><br /><br /></li>
<? 
}
?>

 

what I am wanting to try and achieve, is to go through the rss file, and only display items that have not reached the expiry date.

ie: if (todaysdate <= expirydate) { show item }

but I dont know how to do that, and how to get the date information from the rss file to use it in the php...

 

Can anyone help me??

 

Cheers

Chris

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