how to display only 5 records?

the easiest way would be to stop your loop after 5 iterations:

$i = 0;
foreach($x->channel->item as $entry) {      
  // do something
  $i++;
  if($i==5){
    break;
  }  
}

another (more beautiful) way would be to use a for-loop instead of foreach:

for($i=0; $i<=min(5, count($x->channel->item)); $i++) {   
  $entry = $x->channel->item[$i];
  // do something
}

EDIT :
thanks to Juhana, i changed the code to take that into account.

Leave a Comment