Solved

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

Posted on 2010-09-07
4
570 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:GodsHand
  • 2
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:inxil
inxil earned 500 total points
Comment Utility
Looks like you can just do:

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

return $result;
0
 

Author Comment

by:GodsHand
Comment Utility
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

0
 
LVL 7

Accepted Solution

by:
inxil earned 500 total points
Comment Utility
Just change:

 $result .= '<div' . (0 == $i ? ' class="top_story"' : '') . '>';

To:

 $result .= '<div' . ($story == $i ? ' class="top_story"' : '') . '>';

And that should work.
0
 

Author Comment

by:GodsHand
Comment Utility
I owe you big time. I wish you could donate on here.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now