Link to home
Start Free TrialLog in
Avatar of GodsHand
GodsHandFlag for Canada

asked on

foreach loop: Do something different to every item EXCEPT the first

I'm using a foreach to loop through the results of an XML file. I'm loading it through a URL like this:

/feed/get_feed/0 where 0 equals the story I want to start displaying at. So if there are ten stories, and I put 0, the display will show all feeds starting at 0 (which is the first story). If I change it to 1, it displays all feeds starting at the second story. Makes sense.

But now I'm trying to say in the foreach loop "if this is the first story, do this. Else, do this."

		$i = 0;
		foreach ($xml->channel->item as $item):
			if ($i >= $story):
				if ($i == $i) 
				$result .= '<div>';
				$result .= '<h1>'.$item->title.'</h1>';
				$result .= '<p>'.$item->description.'</p>';
				$result .= '</div>';
			endif;
			$i++;
		endforeach;
	
		return $result;

Open in new window


So how do I do that? I basically want to change the <div> to say <div class="top_story"> when it's the first story, and <div class="story"> when it's not the top story.
SOLUTION
Avatar of inxil
inxil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GodsHand

ASKER

That appears to work, exactly what I needed.

But now I have an issue when these are displaying on the page. So when I first load the page, it shows the top story and all the side stories. But, after my 2 second interval is up, the top story disappears, and one of the side stories disappears. What SHOULD be happening is that the top story disappears and the side story moves to the top story.

Here's my javascript below. Any idea why that might be?
<script type="text/javascript">
var numOfFeeds  = 10;
var currentFeed = -1;
var updateInt   = 2;
var cachedResult = null;

$(document).ready(function()
{
	updateFeed();
	var refreshId = setInterval(updateFeed, updateInt*1000); 
});

function updateFeed()
{
	if (++currentFeed >= numOfFeeds) currentFeed = 0;
	var loadUrl = '<?php echo site_url("feed/get_feed"); ?>/' + currentFeed;
	
	$.get(loadUrl, function(result)
	{
		if(result != cachedResult)
		{
			$('#feed').html(result);
			cachedResult = result;
		}
	});
}
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I owe you big time. I wish you could donate on here.