GodsHand
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."
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.
/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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I owe you big time. I wish you could donate on here.
ASKER
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?
Open in new window