Jump to content

*Newbie* Displaying 2 Array Items at Once, Then Loop To Get The Rest...


Recommended Posts

First off, let me apologise for asking such a newbie question...

 

This is my code....

 


// Fetch RSS feed
$rss = fetch_rss('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_div_3/rss.xml');

if ($rss)
{

// Split the array to show first 10
$items = array_slice($rss->items, 0, 10);

// Cycle through each item and echo
foreach ($items as $item ) 

{
  echo '<li><a href="'.$item['link'].'">'.$item['title'].'</a> - '.$item['description'].'</li>';
}
}
else 
{
  echo '<h2>Error:</h2><p>'.magpie_error().'</p>';
}

 

It's basically displaying information from an RSS feed on my page. However I want to display 2 pieces of that array at once, then in the next loop, get the next 2 arrays, and so on and so forth.

Hi,

 

I would use a for loop instead of a foreach loop as it'll give you a bit more control in this situation. The for loop is based on a counter that'll get incremented by 2 each time round the loop. You can then access array elements $i and $i+1 for the two-at-a-time approach you were after.

 

for ($i = 0; $i < 10; $i += 2)
{
   $item1 = $items[$i];
   $item2 = $items[$i + 1];

   // Do something with $item1 and $item2
}

 

cheers,

Darren.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.