Solved

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

Posted on 2010-09-07
4
571 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

816 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