Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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