Iterating through the item elements

Once you have a reference to a channel, you will want to loop through the items in that channel and extract the relevant information. The process is almost identical to looping through the channels of an RSS document.

First you get the number of items in the channel using the getItemCount method of a channel:


//iterate through item elements
$numItems = $currentChannel->getItemCount();	

Then you loop through each item, using the getItem($index) method to grab a reference to each item:


//iterate through item elements
$numItems = $currentChannel->getItemCount();

for ($j = 0; $j < $numItems; $j++) {
	$currentItem =& $currentChannel->getItem($j);
}

Finally, you use the getTitle, getLink, and getDescription methods to present the data to the user:


//iterate through item elements
$numItems = $currentChannel->getItemCount();

for ($j = 0; $j < $numItems; $j++) {
	$currentItem =& $currentChannel->getItem($j);
	
	//echo item info
	echo "<p><a href=\"" . $currItem->getLink() . 
			"\" target=\"_child\">" . 
			$currItem->getTitle() . "</a> " . 
			$currItem->getDescription() . "</p>\n\n";
}

Any well-formed RSS feed can therefore be viewed with a very small amount of DOMIT! RSS code:


require_once('somepath/xml_domit_rss.php');
$rssdoc =& new xml_domit_rss_document('http://www.somesite.com/rss.xml', 
							'./', 3600);
$numChannels = $rssdoc->getChannelCount();

for ($i = 0; $i < $numChannels; $i++) {
	$currentChannel =& $rssdoc->getChannel($i);
	
	//echo channel info
	echo "<h2><a href=\"" . $currChannel->getLink() . 
				"\" target=\"_child\">" . 
				$currChannel->getTitle() . "</a>";
	echo "  " . $currChannel->getDescription() . "</h2>\n\n";
	
	//iterate through item elements
	$numItems = $currentChannel->getItemCount();

	for ($j = 0; $j < $numItems; $j++) {
		$currentItem =& $currentChannel->getItem($j);
	
		//echo item info
		echo "<p><a href=\"" . $currItem->getLink() . 
			"\" target=\"_child\">" . 
			$currItem->getTitle() . "</a> " . 
			$currItem->getDescription() . "</p>\n\n";
	}
}


Documentation generated by ClassyDoc, using the DOMIT! and SAXY parsers.
Please visit Engage Interactive to download free copies.