Solved

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

Posted on 2010-09-07
4
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:inxil
inxil earned 500 total points
ID: 33620492
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
ID: 33620609
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
ID: 33621371
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
ID: 33621479
I owe you big time. I wish you could donate on here.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

636 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